iOS 可自定義AlertView

YYAlertController

語言:Swift 5.0
原理介紹:利用UIViewController的轉場動畫,通過自定義轉場達到和系統彈出警告框一樣的效果
視圖分爲三類:

  1. 標題和小標題
  2. 按鈕
  3. 添加TextField

視圖佈局:給View寫一個擴展,功能是封裝系統的佈局類
視圖佈局自適應
初始化:各種初始化方法,基本滿足各個場景需要
自定義視圖:支持自定義視圖,且自定義視圖的佈局可以用自動佈局的方式,不會影響最終的顯示效果。自定義佈局中也可以.alert 和 actionSheet兩種類型
場景

  1. 所有類型都支持在UIWindow上顯示
  2. UIViewController應用
  3. 自定義視圖可以自己出發消失時間,降低耦合度hidden

CocoaPods集成

YYAlertControllerGit鏈接

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
target 'TargetName' do
pod 'YYAlertController'
end

工程中使用頁面:import YYAlertController 即可使用

Demo

如果想要完整Demo可以下載:Demo鏈接

樣例代碼

1.Default alert

        let alertView = YYAlertView.init(title: "提示", message: "確定要刪除嗎?")
        //alertView.layer.cornerRadius = 8
        //alertView.layer.masksToBounds = true
//        alertView.buttonDefaultBgColor = UIColor.red
        alertView.addAction(YYAlertAction.init("取消", style: .cancle, handler: { (action) in
            print("取消啦")
        }))
        //循環引用
        alertView.addAction(YYAlertAction.init("確定", style: .defalut, handler: { (action) in
            print("確定")
        }))
        alertView.show(in: self, preferredStyle: .alert)
        //alertView.show(in: self)
        
        //        let alertVC = YYAlertController.init(alertView, preferredStyle: .alert)
//        YYAlertController.initWithAlertView(alertView) //另一種初始方法
//        self.present(alertVC, animated: true, completion: nil)

2.ActionSheet

        let alertView = YYAlertView.init(title: "提示", message: "info")
        alertView.layer.cornerRadius = 8
        alertView.layer.masksToBounds = true
    //        alertView.buttonDefaultBgColor = UIColor.red
        alertView.addAction(YYAlertAction.init("取消", style: .cancle, handler: { (action) in
                print("取消啦")
        }))
        alertView.addAction(YYAlertAction.init("summit", style: .defalut, handler: { (action) in
                print("取消啦")
        }))
        alertView.addAction(YYAlertAction.init("確定", style: .defalut, handler: { (action) in
                print("確定")
        }))
        alertView.show(in: self, preferredStyle: .actionSheet)
  1. ShowInWindow
//文本自適應
        let alertView = YYAlertView.alertViewWithTitle("YYAlertView", message: "A message should be a short, but it can support long message, this is ver heard HHhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh. (NSTextAlignmentCenter)")
        alertView.addAction(YYAlertAction.init("取消", style: .cancle, handler: { (action) in
            
        }))
        alertView.addAction(YYAlertAction.init("確定", style: .defalut, handler: { (action) in
            
        }))
        alertView.showInWindow()
  1. TextField Alert
      let alertView = YYAlertView.init(title: "提示", message: "輸入相關信息")
        alertView.layer.cornerRadius = 8
        alertView.layer.masksToBounds = true
        //        alertView.buttonDefaultBgColor = UIColor.red
        alertView.addTextFieldWithConfigurationHandler { (textField) in
            print(textField.text)
            textField.placeholder = "請輸入手機號"
        }
        alertView.addTextFieldWithConfigurationHandler { (textField) in
            print(textField.text)
            textField.placeholder = "請輸入密碼"
        }
        alertView.addAction(YYAlertAction.init("取消", style: .cancle, handler: { (action) in
                print("取消啦")
        }))
        alertView.addAction(YYAlertAction.init("確定", style: .defalut, handler: { (action) in
                print("確定")
        }))
        alertView.show(in: self, preferredStyle: .alert)
  1. CustomView
//支持自定義視圖,包括利用xib創建的視圖
        let customView = YCustomView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width * 0.7, height: 300))
        titleView.backgroundColor = UIColor.white
        let alertVC = YYAlertController.init(titleView, preferredStyle: .alert)
        alertVC.bcgTapDismissEnable = true
        self.present(alertVC, animated: true, completion: nil)

效果圖如下

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

在這裏插入圖片描述

Example

To run the example project, clone the repo, and run pod install from the Example directory first.
如果想要完整Demo可以下載:https://github.com/YaoChengZhen/AlertViewDemo.git

Requirements

Installation

YYAlertController is available through CocoaPods. To install

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'

target 'TargetName' do
pod 'YYAlertController'
end

Then, run the following command:

$ pod install

Use

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章