更改UISearchBar的textField的背景 [英] Change the background of UISearchBar's textField

查看:74
本文介绍了更改UISearchBar的textField的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在努力解决这个问题。已经到处搜索,但提供的解决方案仅适用于objective-c。其中包含

I'm struggling with this for a while now. Have searched everywhere but the solution provided only works with objective-c. Which consists in

UITextField * txt = [_ searchBar valueForKey:@_ searchField];

虽然有些人可能会说这是受保护的API,Apple可能会拒绝使用此类代码的应用。

Although some might say that this is protected API and Apple might refuse an app with such code.

所以,现在我已经尝试了很多方法来解决这个问题,而且根本没有工作。
我的代码是这样的:

So, now I've tried so many ways to work this out and it's not working at all. My code is this:

searchBar = UISearchBar(frame: CGRectMake(0, 0, 320.0, 30.0))
searchBar.autoresizingMask = UIViewAutoresizing.FlexibleWidth
searchBar.searchBarStyle = UISearchBarStyle.Minimal
searchBar.layer.cornerRadius = 15
searchBar.delegate = self

searchBar.backgroundColor = UIColor.whiteColor()

if floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1{
    // Load resources for iOS 6.1 or earlier
    searchBar.placeholder = "Search";
} else {
    // The following "hack" (if we can call that) breaks the UI for different size devices.
    // Load resources for iOS 7 or later
    searchBar.placeholder = "Search                                         ";
}

这给了我奇怪的结果:

This gives me the bizarre result:

虽然我想要的只是里面的文字字段UISearchBar有一个白色背景,而SearchBar本身的cornerRadius比如15.

While what I want is just the textfield inside the UISearchBar to have a white background and the SearchBar itself have a cornerRadius of, say, 15.

我该怎么做?

非常感谢提前

推荐答案

您必须访问UISearchBar中的UITextField。你可以用
使用 valueForKey(searchField)

You have to access the UITextField inside the UISearchBar. You can do that by using valueForKey("searchField")

var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField

textFieldInsideSearchBar?.textColor = yourcolor
textFieldInsideSearchBar?.backgroundColor = backgroundColor
...

你可以改变任何参数,比如UITextField的textColor或backgroundColor

there you can change any parameters like textColor or backgroundColor of the UITextField

这篇关于更改UISearchBar的textField的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆