ios - UISearchBar.appearance() 在首次启动时不起作用?

标签 ios swift uisearchbar placeholder uiappearance

在我的 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)

毕竟,这是第一次启动时发生的事情:

first launch

文本仍然是灰色的,即使我在 AppDelegate 中将它设置为白色。

当我点击搜索栏时:

clicked

当我点击取消时:

after cancel hit

这就是它应该看起来的样子。我不确定为什么在用户交互之前文本是灰色的。有人有解决方案吗?

最佳答案

你可以试试

UILabel.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()

看看能不能用

关于ios - UISearchBar.appearance() 在首次启动时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107513/

相关文章:

swift - 如何在用户在 Swift 中键入时调整 UITextField 高度

ios - 在 swift UICollectionview 中更新单个单元格

ios - 在 UISearchDisplayController 中添加 UISegmentedControl

ios - UISearchController 搜索栏动画第一次很慢

ios - 文本字段作为搜索栏

ios - MPMusicPlayerController applicationMusicPlayer - 应用程序启动时恢复

ios - 如何从 Firebase 加载引用数组?

ios - 暂停/恢复时实时AVAssetWriter同步音频和视频

ios - 注册 UIDeviceOrientation

加载另一个 Nib 时的 iOS 自动布局