如何得到navigationBar.titleTextAttributes?在 Swift 4.0 中工作?
How get navigationBar.titleTextAttributes? works in Swift 4.0?
我曾经在 swift 3 中使用 NSForegroundColorAttributeName,它在 setter 中工作正常并为 UINavigationBar 获取 titleColor,在升级到 Swift 4 之后我无法获取navigationBar.titleTextAttributes?["NSAttributedStringKey.foregroundColor"] 的值是?颜色。下面是我正在使用的代码。请帮忙...
@IBInspectable var titleColor: UIColor? {
set {
guard let color = newValue else { return }
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: color]
}
get {
return navigationBar.titleTextAttributes?["NSAttributedStringKey.foregroundColor"] as? UIColor
}
}
您需要将 NSAttributedStringKey .foregroundColor
而不是字符串传递给您的 getter:
@IBInspectable var titleColor: UIColor? {
set {
guard let color = newValue else { return }
navigationBar.titleTextAttributes = [.foregroundColor: color]
}
get {
return navigationBar.titleTextAttributes?[.foregroundColor] as? UIColor
}
}
我曾经在 swift 3 中使用 NSForegroundColorAttributeName,它在 setter 中工作正常并为 UINavigationBar 获取 titleColor,在升级到 Swift 4 之后我无法获取navigationBar.titleTextAttributes?["NSAttributedStringKey.foregroundColor"] 的值是?颜色。下面是我正在使用的代码。请帮忙...
@IBInspectable var titleColor: UIColor? {
set {
guard let color = newValue else { return }
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: color]
}
get {
return navigationBar.titleTextAttributes?["NSAttributedStringKey.foregroundColor"] as? UIColor
}
}
您需要将 NSAttributedStringKey .foregroundColor
而不是字符串传递给您的 getter:
@IBInspectable var titleColor: UIColor? {
set {
guard let color = newValue else { return }
navigationBar.titleTextAttributes = [.foregroundColor: color]
}
get {
return navigationBar.titleTextAttributes?[.foregroundColor] as? UIColor
}
}