iOS 封裝全局提示框


很多時候產品都有需求, 要提示框, 提示框提示框!!!  系統的太難看了, (ーー゛)我勒個.  讓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發郵箱

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM