代码之家  ›  专栏  ›  技术社区  ›  Tobias

iOS 11中的UISearchBar位置错误

  •  3
  • Tobias  · 技术社区  · 7 年前

    将我们的代码移植到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];
    }
    

    当使用我们的默认颜色时,事情看起来可能没问题,但搜索栏和ScopeButtons之间的空间可能有点太大: default layout different coloring reveals the problem

    1 回复  |  直到 7 年前
        1
  •  5
  •   Tobias    7 年前

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