UISearchBar and UINavigationItem

故事扮演 提交于 2019-12-04 16:50:47

Actually, there's a really simple solution. All you have to do is create a zero-width view for the back item:

UIView *hackView = [[UIView alloc] initWithFrame:CGRectZero];
UIBarButtonItem *hackItem = [[UIBarButtonItem alloc] initWithCustomView:hackView];      
self.navigationItem.backBarButtonItem = hackItem;
[hackView release];
[hackItem release];

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
self.navigationItem.titleView = searchBar;
[searchBar release];

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.

The following code hides the navigationBar just for this UIViewController:

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

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!

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.

caahab

Maybe apple is using an UISearchDisplayController for doing these kinda things.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!