iOS7 在 UINavigationBar 中添加 UIsearchbar 时未显示取消按钮 [英] iOS7 when UIsearchbar added in UINavigationBar not showing cancel button

查看:28
本文介绍了iOS7 在 UINavigationBar 中添加 UIsearchbar 时未显示取消按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UINavigationBar 上方添加 UISearchBar 并设置 UIsearchbar showsCancelButton YES,在 iOS6 中工作正常,但在 iOS7 中不显示取消按钮.我使用了下面的代码片段

I add UISearchBar above UINavigationBar and set UIsearchbar showsCancelButton YES, work fine in iOS6 but in iOS7 not showing cancel button. I used below code snippet

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 600, 44)];
searchBar.showsCancelButton = YES;
searchBar.translucent = NO;
[searchBar setTintColor:[UIColor redColor]];
searchBar.backgroundColor = [UIColor yellowColor];
[self.navigationController.navigationBar   addSubview:searchBar];

推荐答案

由于某些原因,iOS7 在添加到导航栏时不显示取消按钮.如果您尝试将其设置为导航项的 titleView,也会发生这种情况.

For some reason iOS7 does not show the cancel button when added to a navigation bar. This also happens if you try setting it as the titleView of a navigationItem.

您可以先将 UISearchBar 包装在另一个 UIView 中来规避此问题.这是我如何将其作为 titleView 执行的:

You can circumvent this problem by wrapping the UISearchBar in another UIView first. Here's how I do it as a titleView:

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

这篇关于iOS7 在 UINavigationBar 中添加 UIsearchbar 时未显示取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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