let darkGray = UIColor.darkGray
let lightGray = UIColor.lightGray
let white = UIColor.white
// Navigation bar background.
UINavigationBar.appearance().barTintColor = darkGray
UINavigationBar.appearance().tintColor = lightGray
// Color of typed text in the search bar.
let searchBarTextAttributes: [AnyHashable: Any] = [NSForegroundColorAttributeName: lightGray, NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes as! [String : Any]
// Color of the placeholder text in the search bar prior to text entry.
let placeholderAttributes: [AnyHashable: Any] = [NSForegroundColorAttributeName: white, NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))]
// Color of the default search text.
// NOTE: In a production scenario, "Search" would be a localized string.
let attributedPlaceholder = NSAttributedString( string: "Search", attributes: placeholderAttributes)
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = attributedPlaceholder
在let attributionPlaceholder中,我想出了一个错误,想知道是否有人可以为我揭示这一点?
答案 0 :(得分:2)
let placeholderAttributes: [AnyHashable: Any] = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))]
// Color of the default search text.
// NOTE: In a production scenario, "Search" would be a localized string.
let attributedPlaceholder = NSAttributedString( string: "Lookup", attributes: placeholderAttributes as? [String: Any])
这对我有用,谢谢。
答案 1 :(得分:0)
可以用以下代码替换您的代码:
let placeholderAttributes = ["NSForegroundColorAttributeName": UIColor.white, "NSFontAttributeName": UIFont.systemFont(ofSize: CGFloat(UIFont.systemFontSize))]
// equivalent of: let placeholderAttributes: [String: Any?] = ...
let attributedPlaceholder = NSAttributedString( string: "Search", attributes: placeholderAttributes)
因为在这种情况下,您无法将AnyHashable
转换为String
,或[AnyHashable:Any?]
转换为[String:Any?]
。