【问题标题】:Cancel button is not shown in UISearchBarUISearchBar 中不显示取消按钮
【发布时间】:2015-05-27 06:15:19
【问题描述】:

我有UICollectionView。单击UINavigationBar 中的搜索按钮时,我将UISearchControllersearchbar 添加为UINavigationItem 的标题视图。对于 iPhone,它工作正常。对于 iPad,cancel 按钮未显示。仅搜索栏就占据了整个宽度。

谁能帮我解决这个问题?提前致谢。

【问题讨论】:

  • 您是否在 iPhone 和 iPad 上使用不同的故事板或 xib?
  • 没有。我没有使用故事板或 xib。
  • 尝试设置searchBar.showsCancelButton = YES;
  • 文档明确指出:“对于在 iPad 上运行的应用程序,此属性的值被忽略,并且不显示取消按钮”。我认为这不合适,尤其是对于幻灯片/拆分视图场景。所以我提交了一个雷达:openradar.appspot.com/37498710

标签: ios objective-c uisearchbar uisearchcontroller


【解决方案1】:

iOS7 在添加到导航栏时不显示取消按钮。您可以像这样将搜索栏放在另一个视图中。

UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = YES;
[searchBar sizeToFit];
UIView *viewForSearchBar = [[UIView alloc]initWithFrame:searchBar.bounds];
[viewForSearchBar addSubview:searchBar];
self.navigationItem.titleView = viewForSearchBar;

【讨论】:

【解决方案2】:

我遇到了同样的问题,在 iPhone 上搜索取消显示良好,但在 iPad 上却没有。

UISearchBar 包裹在另一个UIView 中的解决方法对我来说效果不佳,因为它具有不同的外观和错误的旋转宽度。

我的解决方案很简单 - 使用搜索而不取消,并将取消添加为 UIBarButtonItem

【讨论】:

  • 这对我来说似乎是正确的答案。 iPad 不支持取消按钮,因此只需手动将其添加为 UIBarButtonItem
【解决方案3】:
Added rightBarButtonItem with selector will work fine for me. And adding searchBar inside view before setting to navigation title view was not showing properly.
Code:- 
self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: #selector(self.dismissView))
func dismissView() {
        if self.controller?.navigationController?.popViewController(animated: true) == nil {
            self.controller?.dismiss(animated: true, completion: nil)
        }
    }

【讨论】:

    【解决方案4】:

    试试这个。为节目取消按钮添加复选标记。

    【讨论】:

    • OP 提到它适用于 iPhone 但不适用于 iPad
    【解决方案5】:

    根据苹果文档setShowsCancelButton

    在 iPad 上运行的应用程序不会显示取消按钮,即使当 您为 showsCancelButton 参数指定 YES。

    我不确定替代方案,但这是苹果为我们提供的。

    【讨论】:

      【解决方案6】:

      Swift 版本:-

      我尝试了@Nikita Khandelwal 方法,但仍然不适合 ipad 视图。这是快速代码,已作为正确答案给出:-

      let searchBar: UISearchBar = UISearchBar()
      searchBar.showCancelButton = true
      searchBar.placeholder = "Search Your Job Title"
      searchBar.fitToSize()
      searchBar.delegate = self //do not need if you delegate searchBar
      let viewForSearchBar: UIView = UIView(frame: searchBar.bounds)
      viewForSearchBar.addSubview(searchBar)
      self.navigationItem.titleView = viewForSearchBar
      

      ********* 但是还有另一种方法可以正确设置取消按钮并适合视图:-

      1. 将搜索栏设置为导航栏标题视图:-

        let searchBar: UISearchBar = UISearchBar()
        searchBar.showCancelButton = true
        searchBar.placeholder = "Search Your Job Title"
        searchBar.delegate = self //do not need if you delegate searchBar
        self.navigationItem.titleView = searchBar
        
      2. 将 Bar 按钮拖放到视图控制器的右侧并将其命名为 Cancel。

      3. 然后将该按钮连接到此功能:-

        @IBAction func iPadCancelButton(sender: AnyObject) {
               UIApplication.sharedApplication().sendAction("resignFirstResponder", to:nil, from:nil, forEvent:nil)
              self.dismissViewControllerAnimated(true, completion: nil)
        }
        

      【讨论】:

        【解决方案7】:

        对于使用 Xcode 11 构建的 iOS 13,我需要手动设置取消按钮上的显示值,具体取决于搜索控制器是否可见

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-10-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-19
          • 2011-02-16
          • 1970-01-01
          相关资源
          最近更新 更多