【问题标题】:UISearchBar at wrong position in iOS 11UISearchBar在iOS 11中的错误位置
【发布时间】:2017-09-22 13:28:29
【问题描述】:

在 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 之间的空间可能有点太大了: 更改搜索栏的 backgroundColor 揭示出了什么问题:

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

【问题讨论】:

    标签: ios uisearchbar uisearchcontroller ios11 xcode9


    【解决方案1】:

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

    【讨论】:

    • 请进一步澄清。我不明白。
    • UISearchBar 有一个名为 searchFieldBackgroundPositionAdjustment 的属性,它可以满足我的需要。只需将此属性设置为您指定的偏移量。
    猜你喜欢
    • 2018-02-10
    • 2018-03-30
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多