2013-12-21 31 views
0

我在导航栏内放置了一个搜索栏。这里是代码NavigationBar中的UISearchBar在键盘显示时出现偏差

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

+0

定义后“不正确“ –

+0

@ElGuapo搜索框不在前面。我更新了问题,添加了预期的UI。 – fengd

+0

这是Sol吗?在查看代码时我看到了同样的问题。 :D –

回答

1

我已经把navigationBar到前固定此设置searchBar作为第一个响应者

[self.searchController setActive:YES animated:NO]; 
[self.searchController.searchBar becomeFirstResponder]; 
[self.view bringSubviewToFront:self.navigationBar]; //this is the line I add 
相关问题