简体   繁体   English

无法选择带有取消按钮的UINavigationController中的UISearchBar

[英]UISearchBar in UINavigationController with cancel button not selectable

I have added a UISearchBar to the UINavigationController just fine, but as soon as I add the cancel button to the search bar I can no longer select it by touching it. 我已经将UISearchBar添加到UINavigationController很好,但是一旦将取消按钮添加到搜索栏中,我将无法通过触摸它来选择它。

The code I'm using to create the search bar and add it is: 我用来创建搜索栏并添加它的代码是:

UISearchBar *theSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,0.0f,320.0f,0.0f)];
theSearchBar.delegate = self;
[theSearchBar setPlaceholder:@"Search iPad"];
[theSearchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.titleView = theSearchBar;
self.navigationItem.titleView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[theSearchBar release];

If I remove the line to add the cancel button I'm able to focus on the search bar and the search works correctly. 如果删除该行以添加“取消”按钮,则可以专注于搜索栏,并且搜索可以正常进行。 As soon as I put that line in, it displays properly, but I can no longer focus on it. 一旦插入该行,它就会正确显示,但是我不能再专注于它了。

I've tried putting it into a simple UIView container, but that didn't solve the problem either. 我尝试将其放入一个简单的UIView容器中,但这也不能解决问题。

What about adding the cancel button could be causing this to fail? 添加取消按钮会导致失败吗?

UPDATE: I ended up not using the cancel button because I determined it wasn't necessary the way the search bar was being used. 更新:我最终没有使用取消按钮,因为我确定这不是使用搜索栏的必要方式。

I would definitely recommend a UISearchDisplayController: apple docs. 我绝对会推荐UISearchDisplayController: 苹果文档。 It has a cancel button built right in, and by placing it in interface builder you can save a lot of code. 它内置了一个取消按钮,通过将其放置在界面生成器中,您可以节省很多代码。 Less code = less bugs. 更少的代码=更少的错误。

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

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