1.前言
項(xiàng)目中要自定義提示框,參考了一些文章,然后自己寫了一個(gè)。

2實(shí)現(xiàn)
- 系統(tǒng)的UIAlertController的使用
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"這是一個(gè)》> alert" message:@"又如何?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
UIAlertController是繼承UIViewController的。我們定義一個(gè)viewcontroller,然后present。
- 在調(diào)用[self presentViewController:alert animated:YES completion:nil]的時(shí)候,會(huì)發(fā)現(xiàn)系統(tǒng)已經(jīng)給你做好了一個(gè)動(dòng)畫,是從底部往上彈,還有就是原來的view controller(也就是[self presentViewController:alert animated:YES completion:nil]中的self)不見了,我們要的不是這樣的效果。這里presentViewController使用的是Modal轉(zhuǎn)場(chǎng),UIKit 已經(jīng)為 Modal 轉(zhuǎn)場(chǎng)實(shí)現(xiàn)了多種效果,當(dāng) UIViewController的modalPresentationStyle屬性為UIModalPresentationCustom或UIModalPresentationFullScreen時(shí),我們就有機(jī)會(huì)定制轉(zhuǎn)場(chǎng)效果,此時(shí)modalTransitionStyle指定的轉(zhuǎn)場(chǎng)動(dòng)畫將會(huì)被忽略。因此我們需要改寫modalPresentationStyle的默認(rèn)屬性值,默認(rèn)是UIModalTransitionStyleCoverVertical,也就是從下往上,原來的view controller不見了的形式。
+(instancetype)BPAlertControllerWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancel sure:(NSString *)sure action:(void (^)(void))buttonAction{
BPAlertController *alert = [[BPAlertController alloc] init];
alert.modalPresentationStyle = UIModalPresentationCustom;
alert.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
alert.titleText = title;
alert.messageText = message;
alert.cancelText = cancel&&cancel.length>0 ? cancel : @"取消";
alert.sureText = sure;
alert.buttonAction = buttonAction;
return alert;
}