進入App彈出提示框


1 ,聲明變量    

/* 遮罩window */

@property(nonatomic,strong)UIWindow *coverWindow;

/* 彈框 */

@property(nonatomic,strong)UIView *alertView;

/* 彈框按鈕 */

@property(nonatomic,strong)UIButton *nextBtn;

/* 彈框標題 */

@property(nonatomic,strong)UILabel *titleLab;

//kUIScreenHeight  屏幕高度

 

2 , 根據字數設置高度

#pragma mark 彈出公告提示

-(UIView *)alertView

{

NSString *textStr = @"系統將於2017年4月20日17~19點間進行升級,在升級期間系統將停止使用,請你提前處理系統中待處理任務。";

    if (!_alertView) {

        CGRect rect = [textStr boundingRectWithSize:CGSizeMake(kUIScreenWidth -100, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:kPingFang_Regular size:14]} context:nil];

        

        float alertHight = rect.size.height + 55 +85;

//高度小於230   就固定高度為230 

//高度大於   屏幕的高度-130  固定高度 屏幕高度-130

        if (alertHight <= 230) {

            alertHight = 230;

        } else if (alertHight >= kUIScreenHeight-130) {

            alertHight = kUIScreenHeight-130;

        }else {

            alertHight = rect.size.height +40+ 40 +15 +70+15;

        }

        _alertView = [[UIView alloc]initWithFrame:CGRectMake(30, (kUIScreenHeight-alertHight)/2, kUIScreenWidth-60, alertHight)];

        _alertView.backgroundColor = [UIColor whiteColor];

        _coverWindow = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, kUIScreenWidth, kUIScreenHeight)];

        _coverWindow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

        

        _alertView.clipsToBounds = YES;

        _alertView.layer.cornerRadius = 4;

        [_coverWindow addSubview:_alertView];

        _coverWindow.windowLevel = UIWindowLevelNormal;

        _coverWindow.hidden = YES;

        [[UIApplication sharedApplication].keyWindow addSubview:_coverWindow];

        //右上角 X 按鈕

        UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        deleteBtn.backgroundColor = [CommonFunctions colorWithHex:0xffffff];

        [deleteBtn setFrame:CGRectMake(_alertView.frame.size.width-31, 15, 16, 16)];

        [deleteBtn setBackgroundImage:[UIImage imageNamed:@"btn_cls"] forState:UIControlStateNormal];

        [deleteBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        [_alertView addSubview:deleteBtn];

        

        //下一步的按鈕

        UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];

        nextBtn.backgroundColor = [CommonFunctions colorWithHex:0x11a1b4];

        [nextBtn setFrame:CGRectMake(30, _alertView.frame.size.height-70, _alertView.frame.size.width-60, 40)];

        [nextBtn setTitle:buttonText forState:UIControlStateNormal];

        [nextBtn.titleLabel setFont:[UIFont fontWithName:kPingFang_Regular size:16]];

        [nextBtn addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];

        [_alertView addSubview:nextBtn];

        

        //標題 Lab

        UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, _alertView.frame.size.width-20, 20)];

        titleLab.text = titleStr;

        titleLab.textColor = [CommonFunctions colorWithHex:0x666666];

        titleLab.font = [UIFont fontWithName:kPingFang_Regular size:16];

        titleLab.textAlignment = NSTextAlignmentCenter;

        [_alertView addSubview:titleLab];

        

        //顯示內容的textView (也可以用lab)

        UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(20, 40+15, _alertView.frame.size.width-40, alertHight-40-15-70-15)];

        textView.text = textStr;

        textView.textColor = [CommonFunctions colorWithHex:0x999999];

        textView.font = [UIFont fontWithName:kPingFang_Regular size:14];

        textView.editable = NO;

        textView.bounces = NO;

        [_alertView addSubview:textView];

    }

    return _alertView;

}

3 , 調用   顯示和隱藏

      //顯示

      [self alertView];

                  self.alertView.hidden = NO;

                  self.coverWindow.hidden = NO;

       //隱藏 

       self.alertView.hidden = YES;

         self.coverWindow.hidden = YES;

 

4 ,實現按鈕的方法 就OK了

 


免責聲明!

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



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