【iOS】基于UIAlertController“改造”的自定義樣式對話框

0、 暫時只支持 UIAlertControllerStyleAlert

先來個最終效果圖,看合您的口味和需求,再接著往下看

默認情況下

----只是把系統(tǒng)的圓角給弄沒了,可以設(shè)置** alert.showRoundCorner = true **顯示出來

1、 自定義樣式效果圖
可以設(shè)置背景圖/色
2、 看到這,相信你已經(jīng)有些興趣了,就再看看用法:
// 創(chuàng)建對話框
   EZAlertController *alert = [EZAlertController 
    alertControllerWithTitle:@"title"           
    message:@"message" 
    owner:self 
    preferredStyle:UIAlertControllerStyleAlert //目前只支持Alert樣式
    titleConfigurationHandler:^(EZAlertString *title) {
        title.font = [UIFont boldSystemFontOfSize:24];
        title.color = [UIColor redColor];
        title.alignment = NSTextAlignmentLeft;
    } 
    messageConfigurationHandler:^(EZAlertString *message) {
        message.font = [UIFont boldSystemFontOfSize:16];
        message.color = [UIColor greenColor];
        message.alignment = NSTextAlignmentRight;
    }];
// 是否顯示圓角
    alert.showRoundCorner = false;
// 是否顯示毛玻璃效果
    alert.showBlurEffect = false;
// 背景色/圖
    alert.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
    
// add action 1
    [alert addActionWithTitle:@"action1" handler:^(UIAlertAction *action) {
        NSLog(@"%@",action.title); //點擊按鈕回調(diào)
    } configurationHandler:^(EZAlertAction *action) {
        action.titleAlignment = NSTextAlignmentLeft; //按鈕文字對齊
        action.titleColor = [UIColor orangeColor]; // 按鈕文字顏色
        action.iconImage = [UIImage imageNamed:@"icon"]; //按鈕的圖標(biāo)
    }];

// add action 2
    [alert addActionWithTitle:@"action2" handler:^(UIAlertAction *action) {
        NSLog(@"%@",action.title);
    } configurationHandler:^(EZAlertAction *action) {
        action.titleAlignment = NSTextAlignmentRight;
        action.titleColor = [UIColor redColor];
    }];

// 添加輸入框1
    [alert addTextFieldWithConfigurationHandler:^(EZAlertTextField *textField) {
        textField.placeholder = @"用戶名";
        textField.borderWidth = 1;
    }];

// 添加輸入框2
    [alert addTextFieldWithConfigurationHandler:^(EZAlertTextField *textField) {
        textField.placeholder = @"密碼";
        textField.borderWidth = 2;
    }];

// 顯示對話框
    [alert presentWithAnimated:true completion:nil];
3、 基于UIAlertController的封裝,其實是變相“改造”

如果你想更進一步“改造”,不妨先弄清楚它原始的結(jié)構(gòu)

三維層級圖

或者如下圖可以改造私有API,但有風(fēng)險且蘋果不推薦

調(diào)試層級圖

我是結(jié)合了2種方式,些許私有API + controller封裝,然后“改造”里邊的view樣式,這樣就不會過多設(shè)計私有API而是直接作用于UIView,自然起到了“自定義”的功效

4、 我簡單打印拆分的UIViewController - Alert的層級結(jié)構(gòu)
s _UIAlertControllerView
s1 0 UIView
        s2 0 _UIDimmingKnockoutBackdropView
            s3 0 UIView
            s3 1 UIVisualEffectView
        s2 1 UIView
            s3 0 UIView
                s4 0 _UIAlertControllerShadowedScrollView
                    s5 0 UILabel - title
                    s5 1 UILabel - message
                    s5 2 UIView - unknown
                    s5 3 UIView  - tf views
                          s6 0 UICollectionViewControllerWrapperView
                              s7 0 UICollectionView
                                  s8 0 _UIAlertControllerTextFieldViewCollectionCell
                                      s9 0 UIView
                                          s10 0 UIVisualEffectView
                                          s10 1 UIView
                                                s11 0 _UIAlertControllerTextField - textfield
                    s5 4 UIImageView
                    s5 5 UIImageView
                s4 1 UILabel
                s4 2 UICollectionView
                    s5 0 _UIAlertControllerCollectionViewCell
                        s6 0 UIView
                            s7 0 _UIAlertControllerActionView
                                s8 0 UIView
                                    s9 0 UILabel - btn
                                    s9 1 UILabel
                                s8 0 UIImageView
                                s9 1 UIView
                    s5 1 _UIAlertControllerBlendingSeparatorView
                        s6 0 _UIBlendingHighlightView
                            s7 0 _UIView
                            s7 1 _UIView
                        s6 1 UIImageView
                        s6 2 UIImageView
5、簡單易用,與UIAlertController無異
拖進去

github: https://github.com/BackWorld/EZAlertController

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

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

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