设置tintColor

时间:2015-07-10 15:56:02

标签: ios swift uialertcontroller

我的UIAlertController首选格式ActionSheet运行良好,没有任何问题。为了防止冗长的代码墙,我删除了UIAlertAction闭包中的操作并添加了代码:

let alert = UIAlertController(title: "More actions", message: nil, preferredStyle: .ActionSheet)
//alert.view.tintColor = UIColor(red: CGFloat(252.0/255.0), green: CGFloat(112.0/255.0), blue: CGFloat(87.0/255.0), alpha: 1.0)

alert.addAction(UIAlertAction(title: "Rename", style: .Default, handler: { (action: UIAlertAction!) -> Void in }))
alert.addAction(UIAlertAction(title: "Move", style: .Default, handler: { (action: UIAlertAction!) -> Void in }))
alert.addAction(UIAlertAction(title: "Keep offline", style: .Default, handler: { (action: UIAlertAction!) -> Void in }))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

self.presentViewController(alert, animated: true, completion: nil)

此代码工作正常,结果如下:

enter image description here

然而当我取消注释我设置tintColor的行时,我的.Default按钮与.Cancel按钮之间的距离会缩小:< / p>

enter image description here

有没有办法防止这种情况发生,或者是否是我忽略的一些微小细节的结果?

2 个答案:

答案 0 :(得分:2)

将按钮添加到警报控制器后设置色调颜色似乎有效:

let alert = UIAlertController(title: "More actions", message: nil, preferredStyle: .ActionSheet)

alert.addAction(UIAlertAction(title: "Rename", style: .Default, handler: { (action: UIAlertAction!) -> Void in }))
alert.addAction(UIAlertAction(title: "Move", style: .Default, handler: { (action: UIAlertAction!) -> Void in }))
alert.addAction(UIAlertAction(title: "Keep offline", style: .Default, handler: { (action: UIAlertAction!) -> Void in }))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))

alert.view.tintColor = UIColor(red: CGFloat(252.0/255.0), green: CGFloat(112.0/255.0), blue: CGFloat(87.0/255.0), alpha: 1.0)

self.presentViewController(alert, animated: true, completion: nil)

答案 1 :(得分:2)

需要在样式设置后设置警报视图色调。