在UIAlertController中为UIAlertAction更改backgroundColor

时间:2017-07-19 23:24:07

标签: swift uialertcontroller uialertaction

我的目标是

enter image description here

如何在图像上为swift自定义UIAlertController? 如何为UIAlertAction更改backgroundColor

1 个答案:

答案 0 :(得分:0)

扩展名UILabel { @IBInspectable公共var外观字体: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;
        
    }
  
}

}

并添加用法

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

在appDelegate中

。它将更改所有viewController中警报控制器的默认按钮的背景颜色。