ios - UISearchBar 在 iOS 11 中的错误位置

标签 ios uisearchbar uisearchcontroller ios11 xcode9

在 iOS 11 中采用我们的代码,我正在努力处理位于 UINavigationController 中的 UISearchbar。 我这样设置 SearchController/SearchBar:

self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchBar.scopeButtonTitles = @[@"foo", @"bar"];
self.searchController.searchBar.showsScopeBar = YES;
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.delegate = self;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.definesPresentationContext = YES;
if (@available(iOS 11.0, *))
{
    self.navigationItem.searchController = self.searchController;
    self.navigationItem.hidesSearchBarWhenScrolling = YES;
    UISearchBar *searchbar = self.searchController.searchBar;
    [searchbar setSomeColors];
}

当使用我们的默认颜色时,事情可能看起来不错,但 SearchBar 和 ScopeButtons 之间的空间可能有点太大: default layout 更改搜索栏的 backgroundColor 揭示出了什么问题: different coloring reveals the problem

因此,UISearchBar 在其父 View 中的位置有点太高了。有谁知道,是什么导致了 iOS 11 中的这种行为?

最佳答案

哇,我不知道,这是自 iOS 5 以来就存在的......

[self.searchController.searchBar setSearchFieldBackgroundPositionAdjustment:UIOffsetMake(0.0, 8.0)];

参见UISearchBar.h

@property(nonatomic) UIOffset searchFieldBackgroundPositionAdjustment NS_AVAILABLE_IOS(5_0) UI_APPEARANCE_SELECTOR;

关于ios - UISearchBar 在 iOS 11 中的错误位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46365969/

相关文章:

ios - 数组 Swift 2 的变化

ios - 推送 View Controller 时,UISearchController searchBar 不会消失

ios - UISearchController 显示不正确的结果

ios - 更改 iOS 中 UISearchBar 范围按钮的高度?

swift - 将字符串中的重音字符替换为基本字符 - ą -> a , ć -> c

ios - 如何使用 UISearchController 使导航栏保持可见?

ios - 如何在运行时确定 CloudKit 环境?

ios - 应用程序处理错误 : Unexpected CFBundleExecutable Key

ios - Twitter 应用程序开发有沙盒环境吗?

iOS 7,搜索模式下范围栏变黑