当键盘显示时,NavigationBar中的UISearchBar会被取消

时间:2013-12-21 15:38:14

标签: objective-c cocoa-touch ios7 uisearchbar uiappearance

我在搜索栏中放置了一个搜索栏。这是代码

self.navigationBar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
[self.view addSubview:self.navigationBar];

self.doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                           target:self
                                                           action:@selector(doneButtonPressed)];
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.placeholder = @"Name of City";
self.searchBar.delegate = self;

self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTitle = @"Add Location";
self.searchController.displaysSearchBarInNavigationBar = YES;
self.searchController.navigationItem.rightBarButtonItems = @[self.doneButton];
self.navigationBar.items = @[self.searchController.navigationItem];

但搜索栏的行为不正确。

enter image description here

我希望它看起来像这样 enter image description here

1 个答案:

答案 0 :(得分:1)

我已将navigationBar设置为searchBar作为第一响应者后将[self.searchController setActive:YES animated:NO]; [self.searchController.searchBar becomeFirstResponder]; [self.view bringSubviewToFront:self.navigationBar]; //this is the line I add 置于前面

{{1}}