UISearchBar在iOS 11中的错误位置

时间:2017-09-22 13:28:29

标签: 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的superview位置有点过高。有谁知道,在iOS 11中导致这种行为的原因是什么?

1 个答案:

答案 0 :(得分:3)

哇,我不知道,自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;