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

3

9

I've got a UISearchBar in my UINavigationItem's titleView associated with a UISearchController. When I navigate back, it appears to flash. Anyone seen this before?

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
Sankaran answered 10/7, 2015 at 17:19 Comment(0)
C
4

I had the same problem and I solved in two ways:

First, you can put the searchStyle to Prominent:

searchController.searchBar.searchBarStyle = .Prominent

I wrote it in Swift by the way, the problem with this solution is that the search icon and the text, and the placeholder has a darker color and if the background is a darker color it looks bad.

The second solution I found is this:

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

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

The key is that both the navigation bar and the search bar isn't translucent and that both have the same color.

I hope this helps you

Clydeclydebank answered 24/9, 2015 at 23:20 Comment(1)
Thanks, @Omzarzi! The key for me was this one: _searchController.searchBar.barTintColor= [HN_APP_DELEGATE brandColor];Sankaran
F
4

For me the case with blinking searchBar was caused by not setting the backgroundImage during searchBar setup.

Swift:

searchBar.backgroundImage = UIImage()
Fanechka answered 29/8, 2017 at 10:42 Comment(0)
P
0

The answer from @omarzl didn't work for me... But I found a little workaround. I am posting it here as an answer so maybe it will help someone.

It's very simple and written in Swift 3.0.

So to avoid the strange flash from the UISearchBar, I just hide it when the view disappears :

override func viewWillDisappear(_ animated: Bool) {

    searchBars.isHidden = true

}

... and make it visible again when the view reappears :

override func viewDidAppear(_ animated: Bool) {

    self.searchBars.isHidden = false

}

I know it is not really a solution, but a "workaround". However, it works and makes your app a little more beautiful than having this buggy UISearBar.

Precondemn answered 25/10, 2016 at 8:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.