【问题标题】:iOS - navigation bar displayed when UISearchController is activeiOS - UISearchController 处于活动状态时显示的导航栏
【发布时间】:2016-01-01 21:46:14
【问题描述】:

我以编程方式创建了一个 UISearchController 并将其添加到我的 UIView A。我在同一个 UIView 中也有一个 UITableView。我使用 UISearchController 来搜索我的 UITableView。当你点击一个单元格时,UIView B 会被推送。 UIView A 和 UIView B 都将 UINavigationBar 设置为 HIDDEN。 当我在没有搜索的情况下直接点击 UITableViewCell 时,一切都很完美,并且 UIView B 显示时没有 UINavigationBar。

但是当我使用 UISearchController 搜索并单击 UITableViewCell 时,UINavigationBar 会显示在 UIView B 上,即使我在 viewDidAppear 方法中将其设置为隐藏。

这是我的代码:

BarsSearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
BarsSearchController.searchResultsUpdater = self;
BarsSearchController.dimsBackgroundDuringPresentation = NO;
BarsSearchController.searchBar.delegate = self;
BarsSearchController.searchBar.barTintColor = [UIColor whiteColor];
BarsSearchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
[searchBarView addSubview:BarsSearchController.searchBar];
[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:searchBarView
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0.0]];

[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:searchBarView
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0.0]];

[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:searchBarView
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0.0]];

[searchBarView addConstraint:[NSLayoutConstraint constraintWithItem:BarsSearchController.searchBar
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:searchBarView
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0.0]];
[searchBarView layoutIfNeeded];
self.definesPresentationContext = YES;
[BarsSearchController.searchBar sizeToFit];

我在这里错过了什么?

【问题讨论】:

  • BarsSearchController.hidesNavigationBarDuringPresentation = YES;

标签: ios objective-c uinavigationbar uisearchcontroller


【解决方案1】:
BarsSearchController.hidesNavigationBarDuringPresentation = YES;

【讨论】:

  • 其实,不好意思,这个不行……默认值已经是YES了,导航栏还是显示的。其他想法?
【解决方案2】:

我有同样的问题,为了解决它,我在 viewDidLoad 中隐藏了返回按钮和背景:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.hidesBackButton = YES;
    [[[self navigationController] navigationBar] setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [[[self navigationController] navigationBar] setShadowImage:[UIImage new]];
    [[[self navigationController] navigationBar] setTranslucent:YES];
}

我将 navigationBar 隐藏在 viewDidAppear 中:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [[self navigationController] setNavigationBarHidden:YES];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多