我在搜索栏中放置了一个搜索栏。这是代码
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];
但搜索栏的行为不正确。
我希望它看起来像这样
答案 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}}