隐藏UISearchBar明文按钮

时间:2014-02-03 07:47:27

标签: ios objective-c uitextfield uisearchbar

我想知道如何隐藏或不显示UISearchBar

textField中出现的UISearchBar十字架

我尝试过使用此

filterSearchBar.showsCancelButton = NO;

然而,这是一个实际的取消按钮而不是小的灰色十字架,所以我想知道UISearchBar中显示的小灰色按钮是否等效。

15 个答案:

答案 0 :(得分:33)

您需要获取搜索栏的textField:

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;
希望这有帮助! =)

答案 1 :(得分:8)

有一种更好的方法可以做到这一点,而且您不必使用私有API或遍历子视图来执行此操作,因此此解决方案是App Store安全的。

UISearchBar有一个用于执行此操作的内置API:

[UISearchBar setImage:forSearchBarIcon:state]

你想要的SearchBar图标键是UISearchBarIconClear,你想要UIControlStateNormal状态。然后给它一个清晰的图像图像,你就完成了。

所以,它应该是这样的:

[searchBar setImage:clearImage forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];

答案 2 :(得分:7)

根据@Gines的回答,这是Swift版本:

func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
    guard let firstSubview = searchBar.subviews.first else { return }

    firstSubview.subviews.forEach {
        ($0 as? UITextField)?.clearButtonMode = .never
    }
}

答案 3 :(得分:5)

Swift 4

添加亚历山大的答案并阻止用户在清除按钮上进行互动:

隐藏按钮:

searchBar.setImage(UIImage(), for: .clear, state: .normal)

要在清除按钮上禁用用户互动,只需将UISearchBar子类化

class CustomSearchBar: UISearchBar {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        if view is UIButton {
            return view?.superview // this will pass-through all touches that would've been sent to the button
        }
        return view
    }
}

答案 4 :(得分:4)

您可以删除所有UISearchBar实例的明文按钮:

[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].clearButtonMode = UITextFieldViewModeNever;

答案 5 :(得分:3)

雨燕5

在iOS 13上进行了测试

为我工作的班轮:

searchBar.searchTextField.clearButtonMode = .never

您还可以将其设置为.whileEditing,以便在用户键入时显示它,然后在搜索栏失去焦点时将其删除。

答案 6 :(得分:2)

Swift 3,基于@Alexsander回答:

import Foundation
import StoreKit

class Purchase: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver {

......

 func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

        print("in product request")

        var products = response.products
        if (products.count > 0) {
            for i in 0 ..< products.count
            {
                if products[i].productIdentifier == buyingProduct{
                    buyProduct(products[i])
                    break
                }
            }

        } else {
            print("No products found")

        }

    }


}

答案 7 :(得分:2)

快捷键5

只需在下面添加一行

searchBar.searchTextField.clearButtonMode = .never

答案 8 :(得分:1)

我尝试了关于这个问题的不同解决方案,即使是在这篇文章中选择的那个,但是它们没有用。

这是我找到解决此问题的方法:

UIView *subview = [[searchBar subviews] firstObject]; //SearchBar only have one subview (UIView)

//There are three sub subviews (UISearchBarBackground, UINavigationButton, UISearchBarTextField)
for (UIView *subsubview in subview.subviews)
{
    //The UISearchBarTextField class is a UITextField. We can't use UISearchBarTextField directly here.
    if ([subsubview isKindOfClass: [UITextField class]])
    {
            [(UITextField *)subsubview setClearButtonMode:UITextFieldViewModeNever];
    }
}

答案 9 :(得分:1)

试试这个:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    UITextField *textField = [searchBar valueForKey:@"_searchField"];
    textField.clearButtonMode = UITextFieldViewModeNever;

}

答案 10 :(得分:1)

Swift 3解决方案:

extension UISearchBar{
    var textField : UITextField{
        return self.value(forKey: "_searchField") as! UITextField
    }
}

用法:

searchBar.textField.clearButtonMode = .never

答案 11 :(得分:0)

在Swift 2.3的情况下,只需使用:

var searchBar = UISearchBar();
searchBar.frame = CGRectMake(0, 0, 00, 20))
for subview: UIView in (searchBar.subviews.first?.subviews)!
        {
            if (subview.isKindOfClass(UITextField) )
            {
                let textFieldObject = (subview as! UITextField)
                textFieldObject.clearButtonMode = .Never;
            }
        }

答案 12 :(得分:0)

Swift 2.3,基于@Alexsander回答:

searchBar.setImage(UIImage(named: "SearchClearIcon"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted)
searchBar.setImage(UIImage(named: "SearchClearIcon"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)

答案 13 :(得分:0)

将Soto_iGhost的answer转换为Swift 4:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {

    let textField: UITextField = searchBar.value(forKey: "_searchField") as! UITextField
    textField.clearButtonMode = .never
}

如果出口UISearchBar,则可以在类中的任何位置编写以上代码。

答案 14 :(得分:0)

thijsonline的answer

(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])).clearButtonMode = .never
相关问题