简体   繁体   English

更改 UISearchBar 的 textField 的背景

[英]Change the background of UISearchBar's textField

I'm struggling with this for a while now.我现在为此苦苦挣扎了一段时间。 Have searched everywhere but the solution provided only works with objective-c.到处搜索,但提供的解决方案仅适用于objective-c。 Which consists in其中包括

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

Although some might say that this is protected API and Apple might refuse an app with such code.尽管有人可能会说这是受保护的 API,Apple 可能会拒绝使用此类代码的应用程序。

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:这给了我奇怪的结果:在此处输入图片说明

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 内的文本字段具有白色背景,而 SearchBar 本身的角半径为 15。

How can I do this?我怎样才能做到这一点?

Thanks so much in advance非常感谢提前

You have to access the UITextField inside the UISearchBar.您必须访问 UISearchBar 内的 UITextField。 You can do that by using valueForKey("searchField")您可以通过使用valueForKey("searchField")

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

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

there you can change any parameters like textColor or backgroundColor of the UITextField在那里您可以更改 UITextField 的任何参数,例如 textColor 或 backgroundColor

In Swift 3:在 Swift 3 中:

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

Adding to the above answer.补充上面的答案。 If you want to keep the search bar style UISearchBarStyleMinimal and change the background of UISearchBar's textField, set the textField's borderStyle to UITextBorderStyleNone如果要保持搜索栏样式 UISearchBarStyleMinimal 并更改 UISearchBar 的 textField 的背景,请将 textField 的 borderStyle 设置为 UITextBorderStyleNone

Objective C:目标 C:

self.searchBar.searchBarStyle = UISearchBarStyleMinimal;

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

Swift 5 and iOS 13 Swift 5 和 iOS 13

searchBar.searchTextField.backgroundColor = .white

You can direct change colour of textfield background.您可以直接更改文本字段背景的颜色。

Add an extension to UISearchBar to access its UITextField :UISearchBar添加扩展以访问其UITextField

import UIKit

extension UISearchBar {

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

Then you can set its properties as you'd expect:然后您可以按预期设置其属性:

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

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

I'm not sure why everyone has wrote an essay on this topic.我不知道为什么每个人都写了一篇关于这个主题的文章。 The solution is just two lines strong:解决方案只有两条线:

    extension UISearchBar {
    
        func clearBackground() {
            backgroundImage = UIImage() // Background cleared.
            searchBarStyle = .prominent // Prominent ONLY for clear background.
            searchTextField.backgroundColor = .clear
        }
    
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM