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

3z6pesqy  于 2022-11-23  发布在  Swift
关注(0)|答案(3)|浏览(182)

我是新的ios和开发我的第一个应用程序与swift。这个应用程序包含许多弹出窗口在播放应用程序。我想使这非常有吸引力。
内置的UIAlertcontroller破坏了我的外观和感觉。那么有没有办法完全定制UIAlertController。
我想更改标题和消息字体和颜色以及背景颜色和按钮

  • 谢谢-谢谢
bpzcxfmw

bpzcxfmw1#

你的问题似乎是重复的,但不是重复的。
是的,可以。你可以参考这个代码。

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)

输出:

参考链接:Customise UIAlertController

x7rlezfr

x7rlezfr2#

与@ ChetanPrajapati相同,但为了使代码清晰和易于使用,请考虑我创建的这个自定义警报类。现在,您可以像使用普通UIAlertController一样使用该类,并进行自定义:

class AlertDialogController: UIAlertController {
    public convenience init(title: String?, message: String?){
        self.init(title: title, message: message, preferredStyle: UIAlertController.Style.alert)
        
        // Background color.
        let backView = view.subviews.last?.subviews.last
        backView?.layer.cornerRadius = 16.0
        backView?.backgroundColor = UIColor.white
        backView?.layer.borderWidth = 1
        backView?.layer.masksToBounds = true
        backView?.layer.borderColor = UIColor.red.cgColor
        
        //Title Color
        if let title = title {
            var myMutableString = NSMutableAttributedString()
            myMutableString = NSMutableAttributedString(string: title as String, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 17.0)])
            myMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.green, range: NSRange(location:0, length:title.count))
            self.setValue(myMutableString, forKey: "attributedTitle")
        }
        
        //Message Color
        if let message = message {
            var messageMutableString = NSMutableAttributedString()
            messageMutableString = NSMutableAttributedString(string: message as String, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 17.0)])
            messageMutableString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue, range: NSRange(location:0, length:message.count))
            self.setValue(messageMutableString, forKey: "attributedMessage")
        }
    }
    
    public func addAction(_ action: UIAlertAction, titleColor: UIColor) {
        //Action button color
        action.setValue(titleColor, forKey: "titleTextColor")
        super.addAction(action)
    }
}
3j86kqsm

3j86kqsm3#

我修改了Swift5用户的代码

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

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

  • Option2你需要在你的类中扩展uialertcontroller。(对我来说,选项2工作得很好(我正在把它设置为黑色)。

选项1看起来像是白色图层在背景颜色之上。)*
1.更改按钮字符串(取消和确认)和颜色。

第一个
如果你选择选项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
        }
    }

}

相关问题