1. 程式人生 > >iOS自定義彈窗(UIView)

iOS自定義彈窗(UIView)

    //================================================
    //彈窗
    //================================================
    //中灰背景
    UIView *alertBackView = [[UIView alloc] initWithFrame:parentView.frame];
    alertBackView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.5];
    [parentView addSubview:alertBackView];
    //彈窗
    UIView *alertView = [[UIView alloc] initWithFrame:CGRectMake((parentView.width - kAlertViewWidth) / 2, (parentView.height - kAlertViewHeight) / 2, kAlertViewWidth, kAlertViewHeight)];
    alertView.backgroundColor = [UIColor whiteColor];
    alertView.layer.cornerRadius = 0.02 * kAlertViewWidth;
    [alertBackView addSubview:alertView];
    //文字   
    UILabel *labelAlert = [[UILabel alloc] init];
    labelAlert.lineBreakMode = NSLineBreakByWordWrapping;
    labelAlert.numberOfLines = 0;
    labelAlert.text = @"感謝註冊Hi-service平臺,點選左側\n工具箱中的“邀請同事”按鈕可以快\n速地邀請同事加入。";
    labelAlert.font = [UIFont systemFontOfSize:kContentScaleFactor(24)];
    labelAlert.textColor = [UIColor colorWithHex:0x464646 alpha:1];
    [labelAlert sizeToFit];
    labelAlert.frame = CGRectMake(kContentScaleFactor(50), kContentScaleFactor(60), labelAlert.width, labelAlert.height);
    [alertView addSubview:labelAlert];
    //按鈕
    UIButton *AlertButton = [[UIButton alloc] initWithFrame:CGRectMake(kContentScaleFactor(62), kContentScaleFactor(60+50)+labelAlert.height, kContentScaleFactor(356), kContentScaleFactor(66))];
    [AlertButton setAttributedTitle:[[NSAttributedString alloc] initWithString:@"邀請同事"
                                                                   attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],
                                                                                NSFontAttributeName:[UIFont systemFontOfSize:kContentScaleFactor(28)]}]
                           forState:UIControlStateNormal];
    AlertButton.backgroundColor = kAPPThemeColor;
    AlertButton.layer.cornerRadius = 0.02 * kContentScaleFactor(356);
    [alertView addSubview:AlertButton];

新增單擊手勢事件,removeFromSuperView關閉彈窗。