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

ios - I want to fully customise uialertcontroller as per my app theme

我是ios的新手,正在迅速开发我的第一个应用程序。该应用程序在播放过程中包含许多弹出窗口。我想使它变得非常有吸引力。

内置的UIAlertcontroller破坏了我的外观。因此,有什么方法可以完全自定义UIAlertController。

我想更改标题和消息的字体和颜色,以及背景颜色和按钮。

谢谢。


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

是的,有可能。您可以参考此代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

参考链接:自定义UIAlertController


我为Swift5用户修改了代码

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

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

    Option2,您需要在您的课程中扩展uialertcontroller。
    (对我来说,选项2效果很好(我将其设置为黑色)。
    选项1看起来像是白??色的图层位于背景色的顶部。)

  • 更改按钮的字符串(取消