objective-c - UISearchController 搜索栏位置下降 64 点

标签 objective-c uisearchbar uisearchcontroller

搜索栏恰好出现了 64 点,太低了:

enter image description here

所有其他框架都完全正确。

编辑: - UISearchController 的 View 获取了错误的 origin.y。当它应该为 0 时,它被设置为 64。 如果我添加这个方法:

- (void)didPresentSearchController:(UISearchController *)searchController
{
  [super didPresentSearchController:searchController];
  searchController.view.frame = CGRectMake(0, 0, searchController.view.frame.size.width, searchController.view.frame.size.height);

}

然后 View 对齐。但是,它很笨拙,因为它会跳跃。如果我修改 willPresentSearchController 中的框架,它将不起作用,因为 Controller 必须在其呈现后进行某种布局。

如果我使用 SparkInspector,并从原点 64 编辑 UISearchBarContainerView 的框架(它设置为 0),问题就解决了。

这是我的相关配置:

self.searchResultsController = [[GMSearchTableViewController alloc] init];
self.definesPresentationContext = YES;
self.searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultsController];
self.searchController.dimsBackgroundDuringPresentation = YES;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
[self.view addSubview:self.searchController.searchBar];

我没有使用 Interface Builder,一切都在代码中配置。 我确信设置 definesPresentationContext 是正确的。

VC 位于常规 UINavigationController 中,它位于 SplitViewController 中(但 iPhone 上也存在问题)。

我觉得我缺少一个关于 UINavigationBar

的简单配置选项

我还有一个使用自定义容器 View Controller 模型的不同 Controller ,该模型更复杂,但可以工作。

当我设置

self.definesPresentationContext = NO;

发生这种情况: enter image description here

所以现在 UISearchBar 得到了正确的定位,但是表示上下文是错误的,导致 UISearchController 的 TableView 占据了整个 View 。

最佳答案

以经典方式,我找到了解决方案 ( https://stackoverflow.com/a/30010473/579217 )

这就是诀窍:

- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar
{
  if (bar == self.searchController.searchBar) {
    return UIBarPositionTopAttached;
  }
  else { // Handle other cases
    return UIBarPositionAny;
  }
}

关于objective-c - UISearchController 搜索栏位置下降 64 点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35093042/

相关文章:

ios - 在 hh :mm:ss format to NSDate for Timer Countdown 中转换 NSString

ios - 呈现的 ViewController 中的 subview 在旋转后位置错误

swift - TVOS : Adjust the position of searchbar in UISearchViewController

ios - Swift - 关闭搜索 Controller

objective-c - 尝试使用 NSMutableString 访问可变数组时出现错误访问

objective-c - 位移编码效率(即巧妙的技巧)

ios - 将类作为属性添加到其他

iOS 搜索栏超出 View

ios - Swift - 大标题和搜索栏

swift - UISearchBar textDidChange 来自 plist 的数据