iOS UIAlertController高級自定義

需求:

自定義了無數(shù)的彈窗牡借,發(fā)現(xiàn)系統(tǒng)自帶的有許多私有屬性,隨擴(kuò)展玩玩间驮,實(shí)現(xiàn)以下三種效果:(核心是通過私有 api 屬性 通過 KVC 實(shí)現(xiàn)尘奏;)

效果一:彈窗圖片

Simulator Screen Shot - iPhone 11 Pro - 2021-08-13 at 17.36.24.png
@objc func showActionSheet4(){

        let message = """
        /// 我試圖解決UIAlertController的局限性。
        """;

        let contentView = UIImageView(image: UIImage(named: "Skull.jpg"))
        contentView.contentMode = .scaleAspectFit
        
        UIAlertController(title: "Select date", message: message, preferredStyle: .actionSheet)
            .addActionTitles()
            .setContent(view: contentView, height: 300)
            .present()
    }

效果二:彈窗嵌套導(dǎo)航

WechatIMG63.jpeg
    @objc func showAlertContentVC() {
        let message = "彈窗內(nèi)嵌套了一個(gè)導(dǎo)航控制器";

        UIAlertController(title: "嵌套導(dǎo)航", message: message, preferredStyle: .actionSheet)
            .addActionTitles([kTitleCancell])
            .setContent(vc: UINavigationController(rootViewController: ThirdViewController()), height: 300)
            .present()
    }

效果三:彈窗自定義UIAlertAction

WechatIMG64.jpeg
@objc func showActionSheet7(){
        var list = Array.init(count: 20) { "item_\($0)" }
        list.append(kTitleCancell)

        let alertVC = UIAlertController(title: "請選擇", message: "UIAlertAction 自定義 ", preferredStyle: preferredStyle)
       
        list.map { e in
            if e == kTitleCancell {
                let action = UIAlertAction(title: e, style: .cancel, handler: { action in
                    DDLog(action.title)
                })
                return action
            }

            let action = UIAlertAction(title: "", style: .default, handler: nil)

            let contentView = NNAlertActionView()
            contentView.imageView.image = UIImage(named: "Skull.jpg")
            contentView.textLabel.text = e
            contentView.detailTextLabel.text = "detailTextLabel"

            contentView.addGestureTap { reco in
                action.setValue(NSNumber(booleanLiteral: true), forKey: "checked")
                alertVC.dismiss(animated: true, completion: nil)
                DDLog(e)
            }
            action.setContent(view: contentView, inset: .zero)
            return action
        }.forEach {
            alertVC.addAction($0)
        }
        alertVC.present()
    }

UIAlertController 私有 api 列表及其類型:

_message, @"NSString"
_attributedTitle, @"NSAttributedString"
_attributedMessage, @"NSAttributedString"
_attributedDetailMessage, @"NSAttributedString"
_linkedAlertControllers, @"NSSet"
_cancelAction, @"UIAlertAction"
_actionToKeyCommandsDictionary, 
_keyCommandToActionMapTable, @"NSMapTable"
_resolvedStyle, q
_preferredStyle, q
_contentViewController, @"UIViewController"
_textFieldViewController, 
_backButtonDismissGestureRecognizer, 
_ownedTransitioningDelegate, @
_addContentViewControllerToViewHierarchyNeeded, B
_isInSupportedInterfaceOrientations, B
_isInRecomputePreferredContentSize, B
_batchActionChangesInProgressCount, q
_presenter, @"_UIAlertControllerShimPresenter"
_actionsWithInvokedHandlers, @"NSPointerArray"
_alertControllerStackManager, 
_hidden, B
_springLoaded, B
__shouldFlipFrameForShimDismissal, B
__shouldAllowNilParameters, B
_hasPreservedInputViews, B
_actions, @"NSMutableArray"
_headerContentViewController, @"UIViewController"
_separatedHeaderContentViewController, 
_styleProvider, 
_preferredAction, @"UIAlertAction"
_temporaryAnimationCoordinator, 
_previewInteractionController, 
__visualStyle, @"UIAlertControllerVisualStyle"
_indexesOfActionSectionSeparators, @"NSIndexSet"
__actionDelimiterIndices, @"NSMutableArray"
__compatibilityPopoverController, 
__systemProvidedPresentationView, @"UIView"
__systemProvidedPresentationDelegate, 
_systemProvidedGestureRecognizer, 
_coordinatedActionPerformingDelegate, 
__presentationSourceRepresentationView, @"UIView"
_titleMaximumLineCount, q
_titleLineBreakMode, q

UIAlertAction 私有 api 列表及其類型:

_title, @"NSString"
_titleTextAlignment, q
_enabled, B
_checked, B
_isPreferred, B
_imageTintColor, @"UIColor"
_titleTextColor, @"UIColor"
_style, q
_handler, @?
_simpleHandler, @?
_image, @"UIImage"
_shouldDismissHandler, @?
__descriptiveText, @"NSString"
_contentViewController, @"UIViewController"
_keyCommandInput, @"NSString"
_keyCommandModifierFlags, q
__representer, 
__interfaceActionRepresentation, @"UIInterfaceAction<UIAlertActionMutablePropertyObservering>"
__alertController, @"UIAlertController"

github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末掷酗,一起剝皮案震驚了整個(gè)濱河市调违,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌泻轰,老刑警劉巖技肩,帶你破解...
    沈念sama閱讀 212,383評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異浮声,居然都是意外死亡虚婿,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評論 3 385
  • 文/潘曉璐 我一進(jìn)店門泳挥,熙熙樓的掌柜王于貴愁眉苦臉地迎上來然痊,“玉大人,你說我怎么就攤上這事屉符【缃” “怎么了?”我有些...
    開封第一講書人閱讀 157,852評論 0 348
  • 文/不壞的土叔 我叫張陵矗钟,是天一觀的道長唆香。 經(jīng)常有香客問我,道長吨艇,這世上最難降的妖魔是什么躬它? 我笑而不...
    開封第一講書人閱讀 56,621評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮东涡,結(jié)果婚禮上冯吓,老公的妹妹穿的比我還像新娘倘待。我一直安慰自己,他們只是感情好桑谍,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評論 6 386
  • 文/花漫 我一把揭開白布延柠。 她就那樣靜靜地躺著祸挪,像睡著了一般锣披。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上贿条,一...
    開封第一講書人閱讀 49,929評論 1 290
  • 那天雹仿,我揣著相機(jī)與錄音,去河邊找鬼整以。 笑死胧辽,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的公黑。 我是一名探鬼主播邑商,決...
    沈念sama閱讀 39,076評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼凡蚜!你這毒婦竟也來了人断?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,803評論 0 268
  • 序言:老撾萬榮一對情侶失蹤朝蜘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體枷颊,經(jīng)...
    沈念sama閱讀 44,265評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡夺蛇,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了副渴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片奈附。...
    茶點(diǎn)故事閱讀 38,716評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖煮剧,靈堂內(nèi)的尸體忽然破棺而出斥滤,到底是詐尸還是另有隱情,我是刑警寧澤轿秧,帶...
    沈念sama閱讀 34,395評論 4 333
  • 正文 年R本政府宣布中跌,位于F島的核電站,受9級特大地震影響菇篡,放射性物質(zhì)發(fā)生泄漏漩符。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評論 3 316
  • 文/蒙蒙 一驱还、第九天 我趴在偏房一處隱蔽的房頂上張望嗜暴。 院中可真熱鬧凸克,春花似錦、人聲如沸闷沥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,798評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽舆逃。三九已至蚂维,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間路狮,已是汗流浹背虫啥。 一陣腳步聲響...
    開封第一講書人閱讀 32,027評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留奄妨,地道東北人涂籽。 一個(gè)月前我還...
    沈念sama閱讀 46,488評論 2 361
  • 正文 我出身青樓,卻偏偏與公主長得像砸抛,于是被迫代替她去往敵國和親评雌。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評論 2 350

推薦閱讀更多精彩內(nèi)容