iOS 7不会在导航栏中显示搜索栏的取消按钮 [英] iOS 7 doesn't show cancel button of search bar in navigation bar

查看:99
本文介绍了iOS 7不会在导航栏中显示搜索栏的取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在应该在iOS 6和iOS 7上运行的应用中,如果应用程序在iOS 7上运行,导航栏中嵌入的搜索栏的取消按钮将不再显示。在iOS 6上,它可以正常工作。

In an app that's supposed to run on iOS 6 and iOS 7, the cancel button of the search bar embedded in the navigation bar is not shown anymore if the app is run on iOS 7. On iOS 6, it works.

搜索栏位于导航栏的标题视图中,如果搜索栏成为第一个响应者,则应显示取消按钮:

The search bar is in the title view of the navigation bar and the cancel button should be shown if the search bar becomes the first responder:

iOS 7

iOS 6

在一个孤立的测试案例中,代码非常简单:

In an isolated test case, the code is very simple:

@interface MyViewController : UITableViewController<UISearchBarDelegate>

@property (nonatomic) IBOutlet UISearchBar* searchBar;

@end


@implementation MyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.titleView = self.searchBar;
}

- (void) searchBarTextDidBeginEditing: (UISearchBar*) searchBar {
    [searchBar setShowsCancelButton: YES animated: YES];
}

@end

这是故意改变吗?我在文档中遗漏的iOS 7?如果是的话,应该选择什么?

Is this a deliberate change in iOS 7 that I missed in the documentation? If yes, what is supposed to be the alternative?

如果没有,我的代码是否犯了错误?

If not, have I made a mistake in my code?

推荐答案

看起来你正在做的一切都正确,但显然Apple已经改变了iOS 7中的一些东西。根据iOS 7中的这个问题,取消按钮不会出现在上UISearchBar 嵌入 UINavigationBar

It looks like you're doing everything correctly, but apparently Apple has changed around some things in iOS 7. According to this SO question in iOS 7 the cancel button doesn't appear on a UISearchBar embedded in a UINavigationBar.

根据开发人员文档 showsCancelButton 属性可能与 setShowsCancelButton:Animated 方法的效果略有不同。尝试这样做:

According to the developer documentation, the showsCancelButton property may have a slightly different effect than the setShowsCancelButton:Animated method. Try doing this:

searchBar.showsCancelButton = YES;
[searchBar setShowsCancelButton:YES animated:YES];

我不确定这是否会产生任何影响。您还可以尝试将代码放在不同的委托方法中:

I'm not sure if that will have any impact. You could also try placing the code in a different delegate method:

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; // return NO to not become first responder
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar; // called when text starts editing

您可能还想查看 iOS 7更新日志 。当添加到 UINavigationBar时,看起来Apple改变了行为或 UISearchDisplayController / UISearchBar / code>。看一下UIKit部分下的最后一个要点(虽然不清楚究竟改变了什么)。

You may also want to checkout the iOS 7 changelog. It looks like Apple changed the behavior or a UISearchDisplayController / UISearchBar when added to a UINavigationBar. Take a look at the last bullet point under the UIKit section (although it isn't clear exactly what was changed).

您可能还想尝试使用 UISerachDisplayController 。更简单的方法是在 UITableView 的标题中嵌入 UISearchBar

You may also want to try using a UISerachDisplayController. What might be even easier is to embed the UISearchBar in the header of a UITableView.

这篇关于iOS 7不会在导航栏中显示搜索栏的取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆