xcode UISearchController searchBar 显示CancelButton 不被尊重

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29976503/
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-15 07:05:53  来源:igfitidea点击:

UISearchController searchBar showsCancelButton not being respected

iosxcodeswift

提问by Kyle Decot

I've added a UISearchController to my application and set it's searchBar to the titleViewof my navigationItem.

我在我的应用程序中添加了一个 UISearchController 并将它的 searchBar 设置titleView为我的navigationItem.

This works but I am seeing the cancel button despite having set showsCancelButtonto false.

这有效,但尽管已设置showsCancelButtonfalse.

    searchController = UISearchController(searchResultsController: searchResultsController)
    searchController.searchResultsUpdater = searchResultsUpdater


    // Configure the searchBar
    searchController.searchBar.placeholder = "Find Friends..."
    searchController.searchBar.sizeToFit()
    searchController.searchBar.showsCancelButton = false

    self.definesPresentationContext = true

    navigationItem.titleView = searchController.searchBar

enter image description here

在此处输入图片说明

采纳答案by Kyle Decot

This appears to be a bug in iOS. The same behavior I've described can be seen in the example project supplied by Apple

这似乎是 iOS 中的一个错误。在 Apple 提供的示例项目中可以看到我所描述的相同行为

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html

The documentation states that the default for this is NObut this doesn't seem to be the case. Setting showsCancelButtonto NOseems to have no effect.

文档指出,这是默认的,NO但似乎并非如此。设置showsCancelButtonNO似乎没有效果。

I have filed a radar for this and am waiting to hear back.

我已经为此提交了一份雷达,正在等待回音。

回答by pbasdf

I agree, it seems like a bug. The problem is that the searchControllerkeeps resetting the showsCancelButtonproperty of the searchBar. I found a solution that involves:

我同意,这似乎是一个错误。问题是searchController不断重置showsCancelButtonsearchBar的属性。我找到了一个解决方案,涉及:

  1. subclassing UISearchBarto ignore setShowsCancelButton.
  2. to make the searchController use that subclass, you have to subclass UISearchController.
  3. And then you find that the searchBar is not triggering the search controller's delegate methods, so you have to trigger them separately...
  1. 子类化UISearchBar以忽略setShowsCancelButton.
  2. 要使 searchController 使用该子类,您必须将UISearchController.
  3. 然后你发现searchBar并没有触发搜索控制器的委托方法,所以你必须单独触发它们......

Convoluted, but it seems to do the trick. You can find the full answer here.

令人费解,但似乎可以解决问题。你可以在这里找到完整的答案

回答by Vitya Shurapov

Easy solution in Swift3- we need to make CustomSearchBarwithout cancel button and then override the corresponding property in new CustomSearchController:

Swift3 中的简单解决方案- 我们需要制作没有取消按钮的CustomSearchBar,然后在新的CustomSearchController 中覆盖相应的属性:

class CustomSearchBar: UISearchBar {

override func setShowsCancelButton(_ showsCancelButton: Bool, animated: Bool) {
    super.setShowsCancelButton(false, animated: false)
}}


class CustomSearchController: UISearchController {

lazy var _searchBar: CustomSearchBar = {
    [unowned self] in
    let customSearchBar = CustomSearchBar(frame: CGRect.zero)
    return customSearchBar
    }()

override var searchBar: UISearchBar {
    get {
        return _searchBar
    }
}}

In MyViewController I initialize and configure searchController using this new custom subclass:

在 MyViewController 中,我使用这个新的自定义子类初始化和配置 searchController:

    var mySearchController: UISearchController = ({
    // Display search results in a separate view controller
    //        let storyBoard = UIStoryboard(name: "Main", bundle: Bundle.main)
    //        let alternateController = storyBoard.instantiateViewController(withIdentifier: "aTV") as! AlternateTableViewController
    //        let controller = UISearchController(searchResultsController: alternateController)
    let controller = CustomSearchController(searchResultsController: nil)
    controller.searchBar.placeholder = NSLocalizedString("Enter keyword (e.g. iceland)", comment: "")
    controller.hidesNavigationBarDuringPresentation = false
    controller.dimsBackgroundDuringPresentation = false
    controller.searchBar.searchBarStyle = .minimal
    controller.searchBar.sizeToFit()
    return controller
})()

回答by Tim Walsh

I had to correct by putting in a little hack...

我不得不通过一些小技巧来纠正......

Setting the alpha to 0.0 on viewDidLoad because he screen will flash.

在 viewDidLoad 上将 alpha 设置为 0.0,因为他的屏幕会闪烁。

Before you ask...willPresentSearchController will not work.

在你问之前......willPresentSearchController 将不起作用。

extension GDSearchTableViewController: UISearchControllerDelegate {
    func didPresentSearchController(searchController: UISearchController) {
        searchController.searchBar.setShowsCancelButton(false, animated: false)
        searchController.searchBar.becomeFirstResponder()
        UIView.animateWithDuration(0.1) { () -> Void in
            self.view.alpha = 1.0
            searchController.searchBar.alpha = 1.0
        }
    }
}

回答by ODB

We wanted the search bar to have no Cancel button initially, but have it appear when the user tapped in the search bar. Then we wanted the Cancel button to disappear if user tapped Cancel, or otherwise the search bar lost first responder.

我们希望搜索栏最初没有取消按钮,但在用户点击搜索栏时显示。然后我们希望取消按钮在用户点击取消时消失,否则搜索栏会丢失第一响应者。

What finally worked for me:

最终对我有用的是:

On create:

创建时:

searchBar.showsCancelButton = NO;

We use a subclass of UISearchBar and override searchBarShouldBeginEditing thusly:

我们使用 UISearchBar 的子类并因此覆盖 searchBarShouldBeginEditing:

-(BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar {
    self.showsCancelButton = YES;
    return YES;
}

We also override resignFirstReponder (in the UISearchBar subclass) thusly:

我们还重写了 resignFirstReponder(在 UISearchBar 子类中):

-(BOOL)resignFirstResponder
{
    self.showsCancelButton = NO;
    return [super resignFirstResponder];
}

回答by Nikita Zernov

You can subclass UISearchBar and override method layoutSubviews

您可以继承 UISearchBar 并覆盖方法 layoutSubviews

super.layoutSubviews()
self.showsCancelButton = false

回答by Heckscheibe

my solution was to set the attribute every time anew when I used the searchcontroller respectively its searchbar. I initialized the searchcontroller lazily without setting the attribute and then did

我的解决方案是每次分别使用 searchcontroller 和它的 searchbar 时重新设置属性。我懒惰地初始化了 searchcontroller 没有设置属性,然后做了

searchController.searchBar.showsCancelButton = false

every time before search began. You could do this in the UISearchControllerDelegate methods i.e...

每次搜索开始之前。您可以在 UISearchControllerDelegate 方法中执行此操作,即..

回答by Massimo Polimeni

I try to help you man but I'm not sure that I find the real problem.

我试着帮助你,但我不确定我是否找到了真正的问题。

According to Apple Documentation:

根据苹果文档

showsCancelButton

boolean property that indicating whether the cancel button is displayed

指示是否显示取消按钮的布尔属性

But for hide the cancel button maybe you should use:

但是为了隐藏取消按钮,您可能应该使用:

setShowsCancelButton(_:animated:)

enter image description here

在此处输入图片说明

I hope that can be helpful.

我希望这会有所帮助。

回答by Jonathan Francis

I would also add

我还要补充

searchController.hidesNavigationBarDuringPresentation = false
searchController.delegate = self
searchController.searchBar.delegate = self

See if assigning those delegates will help.

看看分配这些代表是否会有所帮助。

回答by Mojo66

This worked for me (iOS 10):

这对我有用(iOS 10):

- (void)viewWillAppear:(BOOL)animated {
      [super viewWillAppear:animated];
      self.searchController.searchBar.showsCancelButton = NO;
}