iOS 封裝全域提示框

來源:互聯網
上載者:User

標籤:UI   blog   super   hit   動畫   溝通   ddt   win   取消   

很多時候產品都有需求, 要提示框, 提示框提示框!!!  系統的太難看了, (ーー゛)我勒個.  讓UI給個標準吧我做o(╯□╰)o

其實很簡單 可以根據要求讓它以各種動畫形式出現消失.

 

command+ n 建立UIView 的類 ,命名為  AWPopView

 

AWPopView.h檔案

//取消按鈕點擊事件

typedef void(^cancelBlock)();

 

//確定按鈕點擊事件

typedef void(^sureBlock)();

 

@interface AWPopView : UIView

@property(nonatomic,copy)cancelBlock cancel_block;

@property(nonatomic,copy)sureBlock sure_block;

/**

 *  簡書號:iOS_啊偉  http://www.cnblogs.com/chenwei-dcav/

 *

 *  @param title       標題

 *  @param content     內容

 *  @param sure        確定按鈕內容

 *  @param sureBlock   確定按鈕點擊事件

 *

 *  @return SZKAlterView

 */

+(instancetype)alterViewWithTitle:(NSString *)title

                          content:(NSString *)content

                             sure:(NSString *)sure

                      sureBtClcik:(sureBlock)sureBlock;

 

 

AWPopView.m檔案

#define WIDTH  [UIScreen mainScreen].bounds.size.width

#define HEIGHT  [UIScreen mainScreen].bounds.size.height

 

@interface  AWPopView()

@property(nonatomic,retain)UIView *alterView;

 

@property(nonatomic,retain)UILabel *titleLb;

@property(nonatomic,retain)UILabel *contentLb;

@property(nonatomic,retain)UIButton *sureBt;

 

@property(nonatomic,copy)NSString *title;

@property(nonatomic,copy)NSString *content;

@property(nonatomic,copy)NSString *cancel;

@property(nonatomic,copy)NSString *sure;

 

@end

@implementation AWPopView

-(instancetype)initWithFrame:(CGRect)frame

{

    self=[super initWithFrame:frame];

    if (self) {

        

        //標題

        _titleLb=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 15)];

        _titleLb.textColor=SetColor(111, 208, 199);

        _titleLb.font = [UIFont systemFontOfSize:16];

        _titleLb.adjustsFontForContentSizeCategory = YES;

        [self addSubview:_titleLb];

        _titleLb.sd_layout.leftSpaceToView(self, 22.5).topSpaceToView(self, 15).widthIs(70).heightIs(15);

        //內容

        _contentLb=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_titleLb.frame) + 30, self.bounds.size.width, 15)];

        

        _contentLb.font = [UIFont systemFontOfSize:15];

        [self addSubview:_contentLb];

        _contentLb.sd_layout.leftSpaceToView(self, 22.5).topSpaceToView(_titleLb, 30).widthIs(self.bounds.size.width).heightIs(15);

        //確定按鈕

        _sureBt=[[UIButton alloc]initWithFrame:CGRectMake(self.bounds.size.width/2, CGRectGetMaxY(_contentLb.frame), 120, 40)];

        _sureBt.centerX = self.centerX;

        

        _sureBt.layer.cornerRadius = 5;

        _sureBt.layer.masksToBounds = YES;

        

        _sureBt.backgroundColor = SetColor(111, 208, 199);

        

        [_sureBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

        [_sureBt addTarget:self action:@selector(sureBtClick) forControlEvents:UIControlEventTouchUpInside];

        [self addSubview:_sureBt];

        _sureBt.sd_layout.leftSpaceToView(self, (self.bounds.size.width - 120)/2).bottomSpaceToView(self, 20).widthIs(120).heightIs(40);

        

    }

    return self;

}

#pragma mark----實作類別方法

+(instancetype)alterViewWithTitle:(NSString *)title

                          content:(NSString *)content

                             sure:(NSString *)sure

                      sureBtClcik:(sureBlock)sureBlock;

{

    

    AWPopView *alterView=[[AWPopView alloc]initWithFrame:CGRectMake(0, 0, 345, 200)];

    alterView.backgroundColor=[UIColor whiteColor];

    alterView.center=CGPointMake(WIDTH/2, HEIGHT/2 +20);

    alterView.layer.cornerRadius=5;

    alterView.layer.masksToBounds=YES;

    alterView.title=title;

    alterView.content=content;

    alterView.sure=sure;

    alterView.sure_block=sureBlock;

    return alterView;

}

#pragma mark--給屬性重新賦值

-(void)setTitle:(NSString *)title

{

    _titleLb.text=title;

}

-(void)setContent:(NSString *)content

{

    _contentLb.text=content;

}

-(void)setSure:(NSString *)sure

{

    [_sureBt setTitle:sure forState:UIControlStateNormal];

}

#pragma mark----確定按鈕點擊事件

-(void)sureBtClick

{

    [self removeFromSuperview];

    self.sure_block();

}

 

類也寫完了 , 激動人心的時刻到了, 讓他再任何ViewController 出現消失

 

ViewController.m 檔案

//添加淡黑色背景, 彈框出現其他地方不能點擊

UIWindow * window = [[UIApplication sharedApplication]keyWindow];

                            UIView * BlackView = [[UIView alloc]init];

                            BlackView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];

                            [window addSubview:BlackView];

BlackView.sd_layout.leftSpaceToView(window, 0).topSpaceToView(window, 0).rightSpaceToView(window, 0).bottomSpaceToView(window, 0);//做好適配

 

 AWPopView *lll=[AWPopView alterViewWithTitle:@"系統提示" content:@"需要重新登入" sure:@"確定" sureBtClcik:^{

                                BlackView.alpha = 0.0f;

                                LoginViewController * login = [[LoginViewController alloc]init];

                                [self.navigationController pushViewController:login animated:YES];

                            }];

                            [window addSubview:lll];

直接類方法建立, 就是這麼sou eaey . 彈框裡面的按鈕標題可以去類裡面隨便加  , 如果覺得不夠過癮的iOS 妹子可以加QQ  751045314 ,阿偉不介意我們深入溝通哦,要demo留下QQ發郵箱

 

iOS 封裝全域提示框

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.