0

my target is

enter image description here

How can I customize UIAlertController on the image for swift? How change backgroundColor for UIAlertAction

RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70
Sergey
  • 31
  • 3

1 Answers1

0

extension UILabel{ @IBInspectable public var appearanceFont: UIFont {

    get {
        return self.font
    }
    set {
        if (self.tag == 1001) {
            return;
        }

   
        let isBold : Bool = (self.font.fontDescriptor.symbolicTraits.contains(.traitBold))
        let colors : [CGFloat] = self.textColor.cgColor.components!
        
if (self.font.pointSize == 14) {
    // set font for UIAlertController title
    self.font = [UIFont systemFontOfSize:11];
} else if (self.font.pointSize == 13) {
    // set font for UIAlertController message
    self.font = [UIFont systemFontOfSize:11];
} else if (isBold) {
    // set font for UIAlertAction with UIAlertActionStyleCancel
    self.font = [UIFont systemFontOfSize:12];
} else if ((*colors) == 1) {
    // set font for UIAlertAction with UIAlertActionStyleDestructive
    self.font = [UIFont systemFontOfSize:13];
} else {
    // set font for UIAlertAction with UIAlertActionStyleDefault
    self.font = [UIFont systemFontOfSize:14];
            // set background Color for UIAlertAction with UIAlertActionStyleDefault
            let superView = self.superview?.superview
            print("superView class:\(String.init(describing: superView?.classForCoder))")
           
            if String.init(describing: superView?.classForCoder) == "Optional(_UIAlertControllerActionView)"{

                print("superView.superview class:\(String.init(describing: superView?.superview?.classForCoder))")
              superView?.superview?.backgroundColor = .switchTintColor

            }

        }
        self.tag = 1001;
        
    }
  
}

}

and for usage add

UILabel.appearance(whenContainedInInstancesOf: [UIAlertController.self]).appearanceFont = UIFont.systemFont(ofSize: 14.0)

in appDelegate. It will change the background colour of default Button of alert controller in all viewControllers.