【问题标题】:UISearchBar cancel button not styled if displayed before viewDidAppear如果在 viewDidAppear 之前显示,UISearchBar 取消按钮没有样式
【发布时间】:2016-02-16 16:45:36
【问题描述】:

我正在像这样设置取消按钮的色调:

[UIBarButtonItem appearance].tintColor = [UIColor highlightedTextColor];

如果在viewDidAppear 之后或期间显示取消按钮,则效果很好,例如如果搜索栏获得焦点时显示取消:

但是,如果这是之前执行的,例如在 viewDidLoad 甚至 viewWillAppear 中:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.searchBar setShowsCancelButton:YES animated:NO];
}

那么结果就不那么好了:

文字确实存在,但使用色度计只能检测到非常微弱的阴影。 有没有其他人遇到过这个问题并找到了解决方案? 我能找到的最好的方法是在viewDidAppear 中显示取消按钮。

【问题讨论】:

  • 你试过把它放在didLayoutSubviews中吗?

标签: ios objective-c uisearchbar uiappearance


【解决方案1】:

对我来说它工作正常,我在 UIView 中创建 UISearchBar。 viewDidLoad:

- (void)viewDidLoad {
    [super viewDidLoad];
    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(10.0f, 0.0f, self.view.bounds.size.width-20, 44.0f)];
    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _searchBar.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _searchBar.showsCancelButton = YES;
    //_searchBar.searchBarStyle = UISearchBarStyleMinimal;
    [_searchBar sizeToFit];
    [_searchBar becomeFirstResponder];
    _searchBar.delegate = self;
    _searchBar.barTintColor = color;
    _searchBar.tintColor = color;
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                              [UIColor whiteColor],
                                                                                              NSForegroundColorAttributeName,
                                                                                              nil]
                                                                                    forState:UIControlStateNormal];
    _barWrapper = [[UIView alloc]initWithFrame:self.navigationController.navigationBar.bounds];
    [_barWrapper addSubview:_searchBar];
    _barWrapper.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}

在 viewWillAppear 中,我将 barWrapper 放在导航栏中。

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar addSubview:_barWrapper];
    [_searchBar becomeFirstResponder];
}

最后,在 viewWillDisappear 中,我从导航栏中删除 barWrapper。

- (void)viewWillDisappear:(BOOL)animated{
    [_barWrapper removeFromSuperview];
}

【讨论】:

  • 嗨,它对你有用,因为你也打电话给[_searchBar becomeFirstResponder];。否则,取消按钮看起来像是被禁用了,虽然它不是(可以点击并调用正确的方法)。
  • 没有。我可以删除 becomeFirstResponder 并且布局保持不变。我只使用 becomeFirstResponder 将焦点放在搜索栏上。取消按钮的颜色在UIBarButtonItem appearanceWhenContainedIn 中定义
  • 您好,感谢您花时间回复。这次我实际上复制/粘贴了您的代码并运行它。我能够深入挖掘的是tintColor 不适用于UIBarButtonItem,我想这是我应得的,因为它没有UI_APPEARANCE_SELECTOR 注释。但是,您编写的代码包含一些已弃用的定义。重构它以使用appearanceWhenContainedInInstancesOfClasses,我会接受答案。
  • 对不推荐使用的定义感到抱歉。我使用的是部署目标 7.0。我更新了我的答案,现在它没有针对部署目标 9.2 的警告。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多