改变UISearchBar的textField的背景

21

我已经为此苦苦挣扎了一段时间。到处搜索,但提供的解决方案只适用于objective-c。

UITextField *txt = [_searchBar valueForKey:@"_searchField"];

有人可能会说这是受保护的API,苹果可能会拒绝带有此代码的应用程序。

因此,我已经尝试了很多方法来解决这个问题,但都没有成功。我的代码如下:

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                                         ";
}
这给我带来了奇怪的结果:enter image description here。我想要的只是使 UISearchBar 内的文本字段具有白色背景,而 SearchBar 本身具有圆角,比如说15。我该怎么做?非常感谢您的帮助。

UITextField *searchField = [SearchBar valueForKey:@"_searchField"]; searchField.textColor = [UIColor whiteColor]; 我已经在我的应用程序中使用了这段代码,并且该应用程序已经在App Store上可用。 - Bhoomi Jagani
7个回答

32

你需要访问UISearchBar中的UITextField。你可以通过使用valueForKey("searchField")来实现。

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

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

在那里,您可以更改UITextField的textColor或backgroundColor等任何参数。


15
您应该将样式设置为“突出”或“默认”。如果您喜欢使用“最小化”样式,则“backgroundColor”不起作用。 searchBar.searchBarStyle = UISearchBarStyle.Prominent // 或者 .Default - fatihyildizhan
1
@fatihyildizhan 谢谢,确实非常重要的是不要使用Minimal风格!我在这上面花费了太多时间... - Altimac

18

在 Swift 3 中:

if let txfSearchField = searchController.searchBar.value(forKey: "_searchField") as? UITextField {
        txfSearchField.borderStyle = .none
        txfSearchField.backgroundColor = .lightGray
    }

10

补充以上答案。如果您想保持搜索栏样式UISearchBarStyleMinimal并更改UISearchBar的textField的背景,请将textField的borderStyle设置为UITextBorderStyleNone。

Objective C:

self.searchBar.searchBarStyle = UISearchBarStyleMinimal;

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
    txfSearchField.borderStyle = UITextBorderStyleNone;
    txfSearchField.backgroundColor = yourColor;

我已经搜索了很多,这是真正有效的解决方案! - flame3
我同意。当 UISearchBarStyle 设置为 .minimal 时,此解决方案有效。在 iOS 10.3 上进行了测试。 - yohannes

9

Swift 5和iOS 13

searchBar.searchTextField.backgroundColor = .white

你可以直接更改文本框背景颜色。

2
警告:searchTextField 仅在 iOS 13 及以上版本可用。 - woheras

3
UISearchBar添加一个扩展以访问其UITextField
import UIKit

extension UISearchBar {

    var textField: UITextField? {
        let subViews = subviews.flatMap { $0.subviews }
        return (subViews.filter { $0 is UITextField }).first as? UITextField
    }
}

然后您可以像预期的那样设置它的属性:

searchController.searchBar.textField?.backgroundColor = .red
searchController.searchBar.textField?.tintColor = .yellow

当你没有尝试过答案时,不应该回答问题。例如,在iOS 12中这种方法行不通。 - Claus Jørgensen
@ClausJørgensen,您能详细说明一下“不起作用”是什么意思吗? - Daniel Storm
在我的情况下,searchController.searchBar.textField? 总是为 nil - mask8
此外,我们现在可以执行 serachBar.searchTextField.backgroundColor = .white,因此该答案可能适用于旧的 SDK,但不适用于最近的 SDK。 - mask8

1

searchController.searchBar.searchTextField.backgroundColor = UIColor.white searchController.searchBar.searchTextField.textColor = UIColor.black

搜索控制器的搜索栏的搜索文本框背景颜色为白色,文本颜色为黑色。

0

当使用 iOS 13 或更新版本时,可以直接访问 searchTextField 属性。

然而,只有在将 borderStyle 设置为 .none 时,设置 backgroundColor 才能按预期工作。这会导致一个没有圆角边框的文本字段。要解决此问题,请使用以下代码:

searchController.searchBar.searchTextField.borderStyle = .none
searchController.searchBar.searchTextField.backgroundColor = .white // Or any other UIColor
searchController.searchBar.searchTextField.layer.cornerRadius = 8.0

自iOS 13.0以来,searchTextField属性已经在UISearchBar上可用。 - HangarRash
@HangarRash 你说得对! - Ely

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接