Swift-提示框(UIAlertController)使用

weixin_34050427發表於2018-02-09

樣式一:

2685270-9609a5bb59c6094b.png
image.png
        let alertController = UIAlertController(title: "修改暱稱", message: nil, preferredStyle: UIAlertControllerStyle.alert)
        alertController.addTextField(configurationHandler: { (textField: UITextField!) -> Void in
            textField.placeholder = "請輸入暱稱"
            // 新增監聽程式碼,監聽文字框變化時要做的操作
            NotificationCenter.default.addObserver(self, selector: #selector(self.alertTextFieldDidChange), name: NSNotification.Name.UITextFieldTextDidChange, object: textField)
        })
        let cancelAction = UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel, handler: nil)
        let okAction = UIAlertAction(title: "確認", style: UIAlertActionStyle.default , handler: { (action: UIAlertAction!) -> Void in
            let login = (alertController.textFields?.first)! as UITextField
            let str = login.text
            HWPrint("\(String(describing: str))")
            NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UITextFieldTextDidChange, object: nil)
        })
        okAction.isEnabled = false
        alertController.addAction(cancelAction)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }

    /// 監聽文字改變
    @objc func alertTextFieldDidChange(){
            let alertController = self.presentedViewController as! UIAlertController?
            if (alertController != nil) {
                let login = (alertController!.textFields?.first)! as UITextField
                let okAction = alertController!.actions.last! as UIAlertAction
                if (!(login.text?.isEmpty)!) {
                    okAction.isEnabled = true
                } else {
                    okAction.isEnabled = false
                }
            }
    }

樣式二

2685270-3a2cffd9957e6dcf.png
image.png
        weak var weakSelf = self // 弱引用
        let alertController = UIAlertController()
        alertController.view.tintColor = UIColor.HWColorWithHexString(hex: "2E2E2E", alpha: 1) // 修改顏色
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let directMessagesAction = UIAlertAction(title: "私信", style: .default) { (action) in
            
        }
        let focusOnAction = UIAlertAction(title: "關注", style: .default) { (action) in
            
        }
        alertController.addAction(focusOnAction)
        alertController.addAction(directMessagesAction)
        alertController.addAction(cancelAction)
        weakSelf!.present(alertController, animated: true, completion: nil)

樣式三(單獨修改顏色 注:修改標題與內容失敗待研究)

2685270-7a746c0b4c6a90a2.png
image.png
func show() {
        let actionSheet = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
        //修改title字型及顏色
        let title = "標題"
        let titleStr = NSMutableAttributedString.init(string: title)
        titleStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.orange, range: NSRange.init(location: 0, length: title.count))
        titleStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize:20), range: NSRange.init(location: 0, length: title.count))
        actionSheet.setValue(titleStr, forKey: "attributedTitle")
        
        // 修改message字型及顏色
        let message = "此處展示提示訊息"
        let messageStr = NSMutableAttributedString.init(string: message)
        messageStr.addAttribute(NSAttributedStringKey.foregroundColor, value: UIColor.orange, range: NSRange.init(location: 0, length: message.count))
        messageStr.addAttribute(NSAttributedStringKey.font, value: UIFont.systemFont(ofSize:20), range: NSRange.init(location: 0, length: message.count))
        actionSheet.setValue(messageStr, forKey: "attributedMessage")
        
        
        let cancelAction = UIAlertAction.init(title: "取消", style: .cancel) { (action) in
            
        }
        // titleTextAlignment 位置
        // titleTextColor 顏色
        cancelAction.setValue("808080".hexColor(), forKey: "titleTextColor")
        
        let action = UIAlertAction.init(title: "投訴", style: .default) { (action) in
            
        }
        action.setValue("333333".hexColor(), forKey: "titleTextColor")
        actionSheet.addAction(cancelAction)
        actionSheet.addAction(action)
        UIApplication.shared.keyWindow?.rootViewController?.present(actionSheet, animated: true, completion: { })
    }

相關文章