程式碼demo已在Github開源, MXAlertView, 如果幫助到您,點個星star哈!
MXAlertView
is an easy popView to use !
效果截圖
選擇按鈕一個 | 選擇按鈕兩個 | 選擇按鈕三個 | 自定義accessoryView |
---|---|---|---|
如何使用
基本用法
- (IBAction)alertTypeOneClicked {
[MXAlertView showWithTopTitle:@"提示" bottomTitles:@[@"關閉播放"] content:@"你當前在4G模式,確定要播放?" dataSource:nil completionHandler:nil];
}
- (IBAction)alertTypeTwoClicked {
[MXAlertView showWithTopTitle:@"提示" bottomTitles:@[@"關閉播放", @"前去設定"] content:@"你當前在4G模式,確定要播放?" dataSource:nil completionHandler:nil];
}
- (IBAction)alertTypeThreeClicked {
[MXAlertView showWithTopTitle:@"提示" bottomTitles:@[@"關閉播放", @"繼續播放", @"前去設定"] content:@"你當前在4G模式,確定要播放?" dataSource:nil completionHandler:^(int index, UIButton *sender) {
//selected index is index in the `bottomTitles`
if (index == 0) {
//關閉播放
} else if (index == 1) {
//繼續播放
} else {
//前去設定
}
}];
}
複製程式碼
自定義
設定 dataSource
之後在代理中實現MXAlertViewDataSource
中的accessoryViewForContentInMXAlertView
- (IBAction)alertTypeFourClicked {
[MXAlertView showWithTopTitle:@"提示" bottomTitles:@[@"關閉播放", @"前去設定"] content:@"你當前在4G模式,確定要播放?" dataSource:self completionHandler:^(int index, UIButton *sender) {
//selected index is the same index as title in the `bottomTitles`
if (index == 0) {
//關閉播放
} else if (index == 1) {
//繼續播放
} else {
//前去設定
}
}];
}
- (UIView *)accessoryViewForContentInMXAlertView:(MXAlertView *)alertView {
UIView *accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 4 * 15, 20)];
UIView *timerImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
timerImageView.layer.contents = (__bridge id)[[UIImage imageNamed:@"時鐘.png"] CGImage];
[accessoryView addSubview:timerImageView];
CGRect timerImageViewFrame = timerImageView.frame;
UILabel *timerLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(timerImageViewFrame) + 2, timerImageViewFrame.origin.y, 50, CGRectGetHeight(timerImageViewFrame))];
timerLabel.font = [UIFont systemFontOfSize:15];
timerLabel.textColor = [UIColor colorWithRed:49/255.0 green:194/255.0 blue:124/255.0 alpha:1.0];
timerLabel.text = @"2:00";
[accessoryView addSubview:timerLabel];
return accessoryView;
}
複製程式碼