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

iOS 10“rightBarButtonItem”移动到导航栏之外

  •  0
  • maddie  · 技术社区  · 7 年前

    在我的视图控制器中,我在导航栏中显示搜索控制器。

    /*set appearance of search controller */
        _searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
        _searchController.searchBar.delegate = self;
        _searchController.searchResultsUpdater = self;
        [_searchController.searchBar sizeToFit];
        _searchController.dimsBackgroundDuringPresentation = NO;
        self.definesPresentationContext = YES;
        _searchController.hidesNavigationBarDuringPresentation = NO;
        _searchController.searchBar.searchBarStyle = UISearchBarStyleDefault;
    

    在的帮助下,我还创建了一个排序按钮 rightBarButtonItem .

        /*add drop down sort menu button to navigation bar */
        [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
        self.navigationController.navigationBar.translucent = NO;
        self.navigationItem.titleView = self.searchController.searchBar;
    
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                            style:UIBarButtonItemStyleDone
                                                                           target:self
                                                                           action:@selector(displaySortMenu:)];
    
        self.navigationItem.rightBarButtonItem = rightButtonItem;
        rightButtonItem.image = [UIImage imageNamed:@"Sort"];
    
        self.navigationItem.rightBarButtonItem.imageInsets = UIEdgeInsetsMake(6, 0, 0, 0);
    

    在iOS 11中,排序按钮位于导航栏内的搜索控制器旁边,如下所示: enter image description here

    但是,在iOS 10中,按钮完全移出导航栏: enter image description here

    我的第一个想法是将按钮限制在 willLayoutSubviews ,但因为没有 view 按钮的组件,我无法设置 translatesAutoresizingMaskIntoConstraints NO . 然后,我通过完全移除搜索控制器来检查是否存在将搜索控制器放置在导航栏中的问题。仍然(在iOS 10中)按钮位于导航栏之外: enter image description here

    如何修复 右巴布托主义 在iOS 10及更早版本的导航栏中 ?

    1 回复  |  直到 7 年前
        1
  •  0
  •   maddie    7 年前

    我需要释放为其创建的原始按钮,使其作为一个栏按钮项正确地保留在导航栏中。

    [self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];
        self.navigationController.navigationBar.translucent = NO;
        self.navigationItem.titleView = self.searchController.searchBar;
    
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@""
                                                                        style:UIBarButtonItemStyleDone
                                                                       target:self
                                                                       action:@selector(displaySortMenu:)];
    rightButtonItem.image = [UIImage imageNamed:@"Sort"];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release]; // <-- this is the new line!