【发布时间】:2016-01-14 07:04:59
【问题描述】:
我正在使用 SDCAlertView Cocoapod 并尝试自定义 alertview 和 actionsheet,以便更改cornerradius、actionViewSeparatorColor、默认 textcolor 和 .Destructive textcolor,如下图所示:
我已尝试按照 github 文档中的建议创建类型:
如果您正在寻找更多自定义,请创建一个符合 VisualStyle 的类型并在 AlertController 实例上使用 visualStyle。您还可以将 DefaultVisualStyle 子类化为一组默认值,然后您可以根据需要覆盖这些默认值。
但是没有任何运气。我希望你能提供一个如何做到这一点的例子?
这是我希望在 pod 中覆盖的三件事:
VisualStyle protocol extension:
public var actionViewSeparatorColor: UIColor { return UIColor(red: 142/255.0, green: 54/255.0, blue: 65/255.0, alpha: 0.4) }
public func textColor(forAction action: AlertAction?) -> UIColor {
if action?.style == .Destructive {
return Style.maincolor()
} else {
return Style.mainTextColor()
}
}
DefaultVisualStyle Class:
public var cornerRadius: CGFloat {
if #available(iOS 9, *) {
return 6
} else {
return self.alertStyle == .Alert ? 6 : 4
}
}
为了回答您的问题,在 cmets 中,我尝试了以下方法:
import UIKit
import SDCAlertView
public class AlertViewStyle: VisualStyle {
private let alertStyle: AlertControllerStyle
init(alertStyle: AlertControllerStyle) { self.alertStyle = alertStyle }
public var width: CGFloat { return self.alertStyle == .Alert ? 270 : 1 }
public var cornerRadius: CGFloat {
if #available(iOS 9, *) {
return 6
} else {
return self.alertStyle == .Alert ? 6 : 4
}
}
public var margins: UIEdgeInsets {
if self.alertStyle == .Alert {
if #available(iOS 9, *) {
return UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 0)
} else {
return UIEdgeInsetsZero
}
} else {
if #available(iOS 9, *) {
return UIEdgeInsets(top: 30, left: 10, bottom: -10, right: 10)
} else {
return UIEdgeInsets(top: 10, left: 10, bottom: -8, right: 10)
}
}
}
public var actionViewSize: CGSize {
if #available(iOS 9, *) {
return self.alertStyle == .Alert ? CGSize(width: 90, height: 44) : CGSize(width: 90, height: 57)
} else {
return CGSize(width: 90, height: 44)
}
}
public func font(forAction action: AlertAction?) -> UIFont {
switch (self.alertStyle, action?.style) {
case (.Alert, let style) where style == .Preferred:
return UIFont.boldSystemFontOfSize(17)
case (.Alert, _):
return UIFont.systemFontOfSize(17)
case (.ActionSheet, let style) where style == .Preferred:
return UIFont.boldSystemFontOfSize(20)
case (.ActionSheet, _):
return UIFont.systemFontOfSize(20)
}
}
public func textColor(forAction action: AlertAction?) -> UIColor {
if action?.style == .Destructive {
return Style.mainColour
} else {
return Style.mainTextColour
}
}
public var actionViewSeparatorColor: UIColor { return UIColor(red: 142/255.0, green: 54/255.0, blue: 65/255.0, alpha: 0.4) }
}
然后我尝试通过以下方式在视图控制器中创建警报时将其变为现实:
let alert = AlertController(title: title, message: message, preferredStyle: .Alert)
let alertStyle: AlertControllerStyle = ???
alert.visualStyle = AlertViewStyle(alertStyle)
我只是在玩。我尝试了一大堆不同的东西来尝试让它编译但没有运气。我从 SDCAlertView github 问题中看到有人成功自定义了警报和操作表视图。根据上面的代码,您对我应该如何进行有任何建议。
任何见解将不胜感激。
非常感谢,
亚历克西斯
【问题讨论】:
-
您能否提供一些您尝试过的实际代码?因为它是行不通的,因为您不仅必须覆盖视觉样式,而且还必须将该视觉样式分配给警报控制器。
标签: swift ios9 uialertview sdcalertview