簡體   English   中英

隱藏 UISearchBar 明文按鈕

[英]Hide UISearchBar clear text button

我想知道如何隱藏或不顯示出現在UISearchBartextFieldUISearchBar十字

我試過用這個

filterSearchBar.showsCancelButton = NO;

然而,這是一個實際的取消按鈕,而不是灰色的小十字,所以我想知道UISearchBar中顯示的灰色小按鈕是否有等價物。

您需要獲取搜索欄的 textField:

UITextField *textField = [searchBar valueForKey:@"_searchField"];
textField.clearButtonMode = UITextFieldViewModeNever;

希望這有幫助! =)

有一個更好的方法可以做到這一點,而且您不必使用私有 API 或遍歷子視圖來做到這一點,因此該解決方案是 App Store 安全的。

UISearchBar 具有用於執行此操作的內置 API:

[UISearchBar setImage:forSearchBarIcon:state]

你想要的 SearchBar 圖標鍵是 UISearchBarIconClear,你想要 UIControlStateNormal 狀態。 然后給它一個清晰的圖像圖像,你就完成了。

所以,它應該是這樣的:

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

根據@Gines 的回答,這里是 Swift 版本:

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

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

斯威夫特 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
    }
}

斯威夫特 5

在 iOS 13 上測試

一種為我工作的班輪:

searchBar.searchTextField.clearButtonMode = .never

您還可以將其設置為.whileEditing以在用戶鍵入時顯示它,然后在搜索欄失去焦點時將其刪除。

您可以刪除所有 UISearchBar 實例的明文按鈕:

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

Swift 3,基於@Alexsander 的回答:

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

斯威夫特 5

只需在下面添加一行

searchBar.searchTextField.clearButtonMode = .never

斯威夫特 3 解決方案:

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

用法 :

searchBar.textField.clearButtonMode = .never

我針對這個問題嘗試了不同的解決方案,甚至是本文中選擇的解決方案,但它們都不起作用。

這是我發現解決此問題的方法:

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];
    }
}

嘗試這個:

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

}

在 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;
            }
        }

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)

將 Soto_iGhost 的答案轉換為 Swift 4:

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {

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

如果你有UISearchBar的插座,你可以在你班級的任何地方編寫上面的代碼。

thijsonline 的回答很快:

(UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])).clearButtonMode = .never

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM