xcode 禁用 UISearchBar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9169049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:14:38  来源:igfitidea点击:

Disable UISearchBar

iosobjective-cxcodeios5uisearchbar

提问by Tom

I have a -as I hope- very simple question: how to disable an UISearchBar in IOS5 to avoid user interaction? I can't find setEnabled or something like this...

我有一个 - 正如我希望的 - 非常简单的问题:如何在 IOS5 中禁用 UISearchBar 以避免用户交互?我找不到 setEnabled 或类似的东西...

Thanks!

谢谢!

回答by Conrad Shultz

Have you tried:

你有没有尝试过:

[searchBar setUserInteractionEnabled:NO];

?

?

回答by flizit

In addition to setting user interaction, I also adjusted the alpha value as well, to make the appearance unique.

除了设置用户交互,我还调整了 alpha 值,使外观独一无二。

searchbar.alpha = .75;

回答by Mazen Kasser

Try this

尝试这个

// Normal 
self.searchDisplayController.searchBar.userInteractionEnabled = YES;
self.searchDisplayController.searchBar.translucent = YES;
self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleDefault;
self.searchDisplayController.searchBar.backgroundColor = [UIColor clearColor];

// Faded out
self.searchDisplayController.searchBar.userInteractionEnabled = NO;
self.searchDisplayController.searchBar.translucent = NO;
self.searchDisplayController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchDisplayController.searchBar.backgroundColor = [UIColor lightGrayColor];

回答by CodeBender

An extension for Swift 4 that provides functions to enable and disable the UISearchBar. Feel free to adjust the alpha value as you see fit, since color schemes in use can impact the result.

Swift 4 的扩展,提供启用和禁用UISearchBar. 随意调整您认为合适的 alpha 值,因为使用的配色方案会影响结果。

extension UISearchBar {
    func enable() {
        isUserInteractionEnabled = true
        alpha = 1.0
    }

    func disable() {
        isUserInteractionEnabled = false
        alpha = 0.5
    }
}

Then to disable the searchBar:

然后禁用搜索栏:

searchBar.disable()

To enable:

启用:

searchBar.enable()

回答by pableiros

For Swift 3:

对于 Swift 3:

self.searchController.searchBar.isUserInteractionEnabled = true

Or if you want to still detect user interaction you can do this to disable the UITextFieldinside the UISearchBar:

或者,如果您仍想检测用户交互,您可以执行此操作以禁用UITextField内部UISearchBar

(self.searchController.searchBar.value(forKey: "searchField") as! UITextField).isEnabled = false