在我的 AppDelegate 中,我在我的应用程序中自定义了 UISearchBars(我有几个),因此它们会匹配应用程序的颜色等。 (应用程序的颜色有点青色)。我的代码如下:
// Set appearance of UISearchBar for the app appearance
UISearchBar.appearance().tintColor = UIColor.whiteColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = appColor
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UIFont.init(name: "AvenirNext-Medium", size: 17)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
UISearchBar.appearance().searchBarStyle = UISearchBarStyle .Minimal
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = NSAttributedString.init(string: "Search...", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
UISearchBar.appearance().setImage(UIImage(named: "searchIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState .Normal)
毕竟,这是第一次启动时发生的事情:
文本仍然是灰色的,即使我在 AppDelegate 中将它设置为白色。
当我点击搜索栏时:
当我点击取消时:
这就是它应该看起来的样子。我不确定为什么在用户交互之前文本是灰色的。有人有解决方案吗?
最佳答案
你可以试试
UILabel.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
看看能不能用
关于ios - UISearchBar.appearance() 在首次启动时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107513/