为什么 UISearchBar 在向后导航时出现奇怪的闪烁?

Why does the UISearchBar appear to have a strange flash when navigating back?

我的 UINavigationItem 的 titleView 中有一个 UISearchBar 与 UISearchController 关联。当我向后导航时,它似乎在闪烁。有人以前看过这个吗?

vid of flash

@interface HNTileSearchViewController () <HNTileSearchResultsProtocol, SWRevealViewControllerDelegate, UISearchBarDelegate, HNSetSearchFiltersProtocol, HNKeywordResultsProtocol>
...
@property (nonatomic, strong) UISearchController *searchController;
@property (nonatomic, strong) UISearchBar * searchBarTop;
...
@end


@implementation HNTileSearchViewController
...
    - (void) customPreSetup {
        HNKeywordResultsTableViewController * searchResultsController = [self.storyboard instantiateViewControllerWithIdentifier:HNKeywordResultsTableViewControllerStoryboardIdentifier];
        searchResultsController.delegate = self;
        _searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];
        _searchController.searchResultsUpdater = searchResultsController;
        _searchController.hidesNavigationBarDuringPresentation = NO;
        _searchController.dimsBackgroundDuringPresentation = NO;
        _searchBarTop = _searchController.searchBar;
        _searchBarTop.delegate = self;
        _searchBarTop.placeholder = NSLocalizedString(@"Search heynay", nil);
        _searchBarTop.showsCancelButton = NO;
        _searchBarTop.showsScopeBar = NO;
        self.navigationItem.titleView = _searchBarTop;
        self.definesPresentationContext = YES;
    }

    - (void) viewDidLoad {
        [super viewDidLoad];
        [self customPreSetup];
        ...
    }
....
@end

我遇到了同样的问题,我用两种方法解决了:

首先,您可以将searchStyle设置为Prominent:

searchController.searchBar.searchBarStyle = .Prominent

顺便说一下,我在Swift中写了它,这个解决方案的问题是搜索图标和文本,占位符的颜色较深,如果背景颜色较深,则看起来很糟糕.

我找到的第二个解决方案是:

 navigationController!.navigationBar.translucent=false
 navigationController!.navigationBar.barTintColor=UIColor.redColor()

 searchController.searchBar.barTintColor=UIColor.redColor()
 searchController.searchBar.searchBarStyle = .Prominent
 searchController.searchBar.translucent=false

关键是导航栏和搜索栏都不是半透明的,而且都具有相同的颜色。

希望对你有帮助

@omarzl 的回答对我不起作用...但我找到了一些解决方法。我将它作为答案发布在这里,所以也许它会对某人有所帮助。

很简单,用Swift3.0写的。

所以为了避免 UISearchBar 奇怪的闪烁,我只是在视图消失时隐藏它:

override func viewWillDisappear(_ animated: Bool) {

    searchBars.isHidden = true

}

...并在视图重新出现时使其再次可见:

override func viewDidAppear(_ animated: Bool) {

    self.searchBars.isHidden = false

}

我知道这不是真正的解决方案,而是 "workaround"。但是,它可以工作,并使您的应用程序比这个有问题的 UISearBar 更漂亮。

对我来说,搜索栏闪烁的情况是由于在搜索栏设置期间没有设置背景图像造成的。

Swift:

searchBar.backgroundImage = UIImage()