搜索栏在单击时更改位置

时间:2017-08-18 10:03:09

标签: ios objective-c ipad uisearchbar uisearchcontroller

我已经在Objective C中为iOS设备编写了searchBar代码,它在iPhone上运行良好但是在iPad上它正在改变它的x值,因为取消按钮也隐藏了。

最初搜索栏看起来像:

enter image description here

点击搜索栏后:

enter image description here

是否有人面对这个问题并在此向我提出建议?

以下是代码:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];    
self.searchController.searchBar.delegate = self;
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
followTableView.tableHeaderView = self.searchController.searchBar;
UISearchBar.appearance.tintColor = [UIColor whiteColor]; //Cancel button color

[self.searchController.searchBar sizeToFit];

tapGesture = [[UITapGestureRecognizer alloc]init];
[tapGesture addTarget:self action:@selector(tapGestureBtnAction:)];

self.searchController.searchBar.barTintColor = searchBarColor;


-(IBAction)tapGestureBtnAction:(id)sender
{
  [_searchController.searchBar resignFirstResponder];

  [self.view removeGestureRecognizer:tapGesture];
}

提前致谢。

2 个答案:

答案 0 :(得分:2)

这也发生在我身上, 只需将此设置为NO。

self.definesPresentationContext = NO

检查一下:UISearchController Search Bar Position Drops 64 points

答案 1 :(得分:0)

对于Swift 4.x,这对我有用

self.edgesForExtendedLayout = .bottom