自定义每个UISearchBar来自AppDelegate Not Working

时间:2016-11-17 19:23:18

标签: ios swift appdelegate

我在AppDelegate.swift

中有以下代码
func customizeBars() {
        let bartintColor = UIColor(colorLiteralRed: 20/255, green: 160/255, blue: 160/255, alpha: 1)
        UISearchBar.appearance().tintColor = bartintColor
        window!.tintColor = UIColor(red: 10/255, green: 80/255, blue: 80/255, alpha: 1)
}

我在customizeBars()方法中调用此函数(didFinishLaunchingWithOptions):

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        customizeBars()
        return true
}

如果它有所不同,我在UISearchBArDelegate中也有这个:

func position(for bar: UIBarPositioning) -> UIBarPosition {
    return UIBarPosition.topAttached
}

但由于某些原因,当我运行我的应用时,它不会更改我应用中的tintColor UISearchBarswindow色调的export function requireAuthentication(Component) { class AuthenticatedComponent extends React.Component { componentWillMount () { this.checkAuth(this.props.user.isAuthenticated); } componentWillReceiveProps (nextProps) { this.checkAuth(nextProps.user.isAuthenticated); } checkAuth (isAuthenticated) { if (!isAuthenticated) { let redirectAfterLogin = this.props.location.pathname; browserHistory.push(`/login?next=${redirectAfterLogin}`); } } render () { return ( <div> {this.props.user.isAuthenticated === true ? <Component {...this.props}/> : null } </div> ) } } const mapStateToProps = (state) => ({ user: state.user }); return connect(mapStateToProps)(AuthenticatedComponent); } 。有什么我做错了吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码行更改搜索栏颜色 -

&#13;
&#13;
UISearchBar.appearance().barTintColor = UIColor.red    // Change the color here
&#13;
&#13;
&#13;

但是我看到了,色彩(取消按钮,搜索图标和文字颜色)在iOS 10,swift 3中不起作用。 它曾经在旧版本之前工作过。让我试试,如果我能找到一些东西来改变搜索栏的色调。