UISearchBar.appearance() 无法在首次启动时运行?

UISearchBar.appearance() not working on first launch?

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

请看看是否有效