如果在viewDidAppear之前显示,则UISearchBar取消按钮未设置样式

时间:2016-02-16 16:45:36

标签: ios objective-c uisearchbar uiappearance

我设置取消按钮的色调如下:

.k-grid-header .mygrid-header-cell {
  text-align: center;
  display: table-cell; vertical-align: top;
}
.k-grid-header .wrap-header {
  height: auto;
  overflow: visible;
  white-space: normal;
}

如果在[UIBarButtonItem appearance].tintColor = [UIColor highlightedTextColor]; 之后或期间显示取消按钮,则效果很好,例如如果搜索栏获得焦点时显示取消:

Cancel button properly styled

然而,如果之前执行此操作,例如在viewDidLoad中,甚至在viewWillAppear中:

viewDidAppear

然后结果并不好:

Cancel not styled

文本实际上是存在的,尽管使用Color Meter只能检测到非常微弱的阴影。 有没有其他人遇到过这个问题并找到了解决方案? 我能找到的最好的一个是在- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.searchBar setShowsCancelButton:YES animated:NO]; } 显示取消按钮。

1 个答案:

答案 0 :(得分: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];
}