简体   繁体   English

取消按钮未显示在UISearchBar中

[英]Cancel button is not shown in UISearchBar

I have UICollectionView . 我有UICollectionView On clicking search button in UINavigationBar , I am adding the UISearchController 's searchbar as titleview for UINavigationItem . 在点击搜索按钮UINavigationBar ,我加入UISearchControllersearchbar作为titleview的用于UINavigationItem For iPhone it is working properly. 对于iPhone它运作正常。 For iPad the cancel button is not shown. 对于iPad, cancel按钮不会显示。 The Searchbar alone takes the entire width. 仅搜索栏占据整个宽度。

在此输入图像描述

Can anyone help me out on this?. 任何人都可以帮我解决这个问题吗? Thanks in advance. 提前致谢。

iOS7 does not show the cancel button when added to a navigation bar.You can put searchbar in another view like this. 添加到导航栏时,iOS7不显示取消按钮。您可以将搜索栏放在另一个视图中。

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

I had the same problem, on iPhone the search cancel was shown well, but on iPad it didn't. 我有同样的问题,在iPhone上搜索取消显示良好,但在iPad上它没有。

The workaround of wrapping the UISearchBar in another UIView didn't work well for me since it had different appearance and wrong width on rotation. UISearchBar包装在另一个UIView的解决方法对我来说效果不好,因为它在旋转时具有不同的外观和错误的宽度。

My solution is a simple one - use search WITHOUT cancel, and add cancel as a UIBarButtonItem . 我的解决方案很简单 - 使用搜索WITHOUT取消,并添加取消作为UIBarButtonItem

Try this. 试试这个。 Add a checkmark for shows cancel button. 为节目取消按钮添加复选标记。

在此输入图像描述

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)
        }
    }

Swift version :- Swift版本: -

I tried the @Nikita Khandelwal method, but still it doesn't fit for ipad view. 我尝试了@Nikita Khandelwal方法,但它仍然不适合ipad视图。 Here is the swift code, which was given as corrected answer :- 这是快速代码,作为更正的答案给出: -

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

********* But There is another way to set cancel button correctly and fit for the view :- *********但还有另一种方法可以正确设置取消按钮并适合视图: -

  1. Set search bar as the Navigation bar title view :- 将搜索栏设置为导航栏标题视图: -

     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. Drag and drop Bar button to the right side of the view controller & name it as Cancel. 将“条形图”按钮拖放到视图控制器的右侧,并将其命名为“取消”。

  3. Then connect that button to this function :- 然后将该按钮连接到此功能: -

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

As per apple documentation setShowsCancelButton 根据苹果文档setShowsCancelButton

Cancel buttons are not displayed for apps running on iPad, even when you specify YES for the showsCancelButton parameter. 对于在iPad上运行的应用程序,即使为showsCancelButton参数指定YES,也不会显示取消按钮。

I am not sure about the alternate but this is what apple provides us. 我不确定替代品,但这是苹果为我们提供的。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM