ios - 我想根据我的应用主题完全自定义uialertcontroller

时间:2016-05-27 10:14:43

标签: ios swift swift2 uialertcontroller

我是ios的新手,用swift开发我的第一个应用程序。此应用程序包含许多弹出窗口与应用程序播放。我想让它变得非常有吸引力。

内置的UIAlertcontroller破坏了我的外观和感觉。那么有没有办法完全自定义UIAlertController。

我想更改 TITLE AND MESSAGE FONT AND COLOR 以及背景颜色和按钮

感谢。

2 个答案:

答案 0 :(得分:8)

您的问题似乎重复但不重复。

是的,这是可能的。你可以参考这段代码。

let alertController = UIAlertController(title: "Alert Title", message: "This is testing message.", preferredStyle: UIAlertControllerStyle.Alert)
    // Background color.
    let backView = alertController.view.subviews.last?.subviews.last
    backView?.layer.cornerRadius = 10.0
    backView?.backgroundColor = UIColor.yellowColor()

    // Change Title With Color and Font:

    let myString  = "Alert Title"
    var myMutableString = NSMutableAttributedString()
    myMutableString = NSMutableAttributedString(string: myString as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
    myMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.redColor(), range: NSRange(location:0,length:myString.characters.count))
    alertController.setValue(myMutableString, forKey: "attributedTitle")

    // Change Message With Color and Font

    let message  = "This is testing message."
    var messageMutableString = NSMutableAttributedString()
    messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 18.0)!])
    messageMutableString.addAttribute(NSForegroundColorAttributeName, value: UIColor.greenColor(), range: NSRange(location:0,length:message.characters.count))
    alertController.setValue(messageMutableString, forKey: "attributedMessage")


    // Action.
    let action = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
    action.setValue(UIColor.orangeColor(), forKey: "titleTextColor")
    alertController.addAction(action)
    self.presentViewController(alertController, animated: true, completion: nil)

输出

enter image description here

参考链接: Customise UIAlertController

答案 1 :(得分:0)

我为 Swift5 用户修改了代码

您可以通过以下代码实现什么?

  1. 您可以设置消息的背景颜色(有 2 个选项。)

    Option2 你需要在你的类中扩展 uialertcontroller。 (对我来说,选项 2 效果很好(我将其设置为黑色)。
    选项 1 看起来像是在背景颜色之上有一个白色图层。)

  2. 更改按钮字符串(取消和确认)和颜色。

enter image description here

let alert = customDialog(title: "title test ", message: "message test")
self.present(alert, animated: true)

    func customDialog(title:String, message : String) -> UIAlertController {
        let alertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)

//Background color option1.
            let backView = alertController.view.subviews.last?.subviews.last
            backView?.layer.cornerRadius = 10.0
        backView?.backgroundColor = UIColor.yellow
   
//Background option2. (u need to extend uialertcontroller )
/*
alertController.setBackgroundColor(color: UIColor.yellow)

*/
 
            // Change Title With Color and Font:
    
            var myMutableString = NSMutableAttributedString()
        myMutableString = NSMutableAttributedString(string: title as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: 18.0)!])
        myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.red, range: NSRange(location:0,length:title.count))
            alertController.setValue(myMutableString, forKey: "attributedTitle")
    
            // Change Message With Color and Font
    
            var messageMutableString = NSMutableAttributedString()
        messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSAttributedString.Key.font:UIFont(name: "Georgia", size: 18.0)!])
        messageMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange(location:0,length:message.count))
            alertController.setValue(messageMutableString, forKey: "attributedMessage")
    
    
            // Action.
        let okAction = UIAlertAction(title: "Confirm", style: .default, handler: nil)
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        okAction.setValue(UIColor.orange, forKey: "titleTextColor") //ok in orange color
    
        alertController.addAction(okAction)
        alertController.addAction(cancelAction)
        
    
       // self.present(alertController, animated: true, completion: nil)
        
        return alertController
    }



如果您选择选项 2。背景 。请在下面 https://www.swiftdevcenter.com/change-font-text-color-and-background-color-of-uialertcontroller/

extension UIAlertController {

    //Set background color of UIAlertController
    func setBackgroundColor(color: UIColor) {
        if let bgView = self.view.subviews.first, let groupView = bgView.subviews.first, let contentView = groupView.subviews.first {
            contentView.backgroundColor = color
        }
    }

}