UISearchBar 和 UINavigationItem
我似乎无法让 UISearchBar 将其自身定位在导航栏中从最左边到最右边。在 -(void)viewDidLoad 方法中,我有以下代码:
UISearchBar *sb = [[UISearchBar alloc] initWithFrame:self.tableView.tableHeaderView.frame];
sb.delegate = self;
self.navigationItem.titleView = sb;
[sb sizeToFit];
[sb release];
当您构建并运行时,乍一看它看起来很好。然而,仔细观察,您可以看出左侧有一个边距/空间。从总体上看,这不是什么大问题,但当我点击搜索栏开始搜索时,我将取消按钮设置为视图动画。由于搜索栏位置稍微靠右,因此动画很不稳定,并且取消按钮从末端脱落,如下所示: 链接文本
UINavigationItem 看起来就像一个包含三个单元格的表格,第一个和最后一个上有一个填充,我无法删除 - 似乎也没有办法将它们“合并”在一起,然后将搜索栏放在那里。我知道这种外观是可能的,因为 AppStore 搜索在导航栏中有一个搜索栏,并且它一直延伸到边缘。有人知道如何让搜索栏一直到边缘,以便我的滑入式取消按钮动画能够正常工作吗?
I can't seem to get a UISearchBar to position itself from the far left to the far right in the navigation bar. In the -(void)viewDidLoad method, I have the following code:
UISearchBar *sb = [[UISearchBar alloc] initWithFrame:self.tableView.tableHeaderView.frame];
sb.delegate = self;
self.navigationItem.titleView = sb;
[sb sizeToFit];
[sb release];
When you build and run, it looks just fine at first glance. However, looking more closely, you can tell there is a margin/space on the left. This wouldn't be a big deal in the grand scheme of things, but when I tap the search bar to start a search, I animate the cancel button into view. Because the search bar is positioned slightly to the right, the animation is jerky and the cancel button falls off the end like so:
link text
It seems as if the UINavigationItem is like a table with three cells, where there is a padding on the first and last which I can't remove - nor does there seem to be a way to 'merge' it all together and then place the search bar there. I know this look is possible, because the AppStore search has a search bar in the navigation bar and it goes all the way to the edges. Anyone know how to get the search bar to go all the way to the edges so my slide-in cancel button animation will work properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上,有一个非常简单的解决方案。您所要做的就是为后面的项目创建一个零宽度视图:
请务必在您的 loadView 方法中执行此操作,而不是 init。我不确定为什么这会产生影响,但确实如此。显然,这是关于时机的。将它放在 loadView 中对我来说不再起作用,但是将它放在 viewWillAppear 中可以工作(当然,进行检查,以便只完成一次)。我认为这个想法是在一些初始化完成后设置 titleView 。
Actually, there's a really simple solution. All you have to do is create a zero-width view for the back item:
Be sure to do this in your loadView method, not init. I'm not sure why that makes a difference, but it does.Apparently it's about timing. Having it in loadView stopped working for me, but putting it in viewWillAppear works (with a check so that it's only done once, of course). I think the idea is to set the titleView after some initialization has already completed.
以下代码仅针对此 UIViewController 隐藏导航栏:
因此,要使 UISearchBar 显示在 UINavigationBar 的位置,在根视图控制器上只需将搜索栏放在导航栏通常所在的位置即可!
The following code hides the navigationBar just for this UIViewController:
So to get the UISearchBar to show in the UINavigationBar's place, on your root view controller just have your search bar where the navigation bar would be normally!
我想我找到了答案 - 尽管我还没有进行测试来验证。在我上面提供的问题中,我具有以下结构:
选项卡栏控制器 ->导航控制器->视图控制器
有问题的搜索栏位于视图控制器中,视图控制器又位于导航控制器中,该导航控制器位于选项卡栏中。
我正在随意观看斯坦福CS 193P(2009年春季)课程,在第13讲结束时,答案可能已经呈现。 Alan Cannistraro 表示 Presence 应用程序的结构应具有以下结构:
此结构 http:// /img143.imageshack.us/img143/6/viewcontrollerstruct.jpg
其中底部视图控制器(与选项卡栏控制器相邻)是具有搜索栏控件的视图控制器。他警告说,如果不这样做,你就会“遇到问题”。可能是我遇到的问题?我相信是这样。
I think I found out the answer - though I haven't tested to verify. In the issue I provided above, I have the following structure:
tab bar controller -> navigation controller -> view controller(s)
The search bar in question was in a view controller, which in turn was in the navigation controller, which navigation controller is in the tab bar.
I was casually watching the Stanford CS 193P (Spring 2009) courses and at the end of Lecture 13, the answer may have been presented. Alan Cannistraro stated that the structure of the Presence app should have this structure:
this structure http://img143.imageshack.us/img143/6/viewcontrollerstructure.jpg
where the bottom view controller (adjacent to the tab bar controller) was the view controller which had the search bar control. He warned if it's not done in this fashion, you'll "run into problems". Possibly the problem I faced? I believe so.
也许苹果正在使用 UISearchDisplayController 来做这些事情。
Maybe apple is using an UISearchDisplayController for doing these kinda things.