IOS 自定義AlertController

1.前言

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


QQ20171122-094248.gif

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;
}

3.GIT地址

我的DEMO

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容