Harpal Singh Jadeja Harpal Singh Jadeja - 4 months ago 49
iOS Question

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

I am new to ios and developing my first application with swift. This app contains many popups during play with app. And i want to make this very attractive.

In-built UIAlertcontroller is spoil my look and feel. So is there any way to fully customize UIAlertController.

I want to change FONT AND COLOR of TITLE AND MESSAGE and also BACKGROUND COLOR and Buttons too.



your question seems duplicate but not duplicate.

Yes it is possible. You can refer this code.

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")
    self.presentViewController(alertController, animated: true, completion: nil)

Output :

enter image description here

Reference link : Customise UIAlertController