2015-07-10 63 views
8

我有一個UISearchBar在我的UINavigationItem的titleView與UISearchController關聯。當我導航回來時,它似乎閃爍。任何人見過這個?爲什麼UISearchBar在導航回來時似乎有一個奇怪的閃光?

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 

回答

4

我有同樣的問題,我用兩種方式解決:

首先,你可以把searchStyle以突出:

searchController.searchBar.searchBarStyle = .Prominent 

我寫在斯威夫特順便說一句,這個解決方案的問題在於搜索圖標和文本,佔位符顏色較深,如果背景顏色較深,它看起來很糟糕。

第二個解決方案,我發現是這樣的:

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

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

關鍵的是,無論是導航欄和搜索欄不具有透光性,並且具有相同的顏色。

我希望這有助於你

+2

謝謝,@Omzarzi!我的關鍵是這一個:'_searchController.searchBar.barTintColor = [HN_APP_DELEGATE brandColor];' –

0

從@omarzl答案對我來說沒有工作......但我發現一個小的解決方法。我在這裏發佈它作爲答案,所以也許它會幫助別人。

這很簡單,用Swift 3.0編寫。

因此,爲了避免從的UIS​​earchBar奇怪的閃光,我只是把它隱藏在視圖中消失:

override func viewWillDisappear(_ animated: Bool) { 

    searchBars.isHidden = true 

} 

...並使其可見時再重新出現觀點:

override func viewDidAppear(_ animated: Bool) { 

    self.searchBars.isHidden = false 

} 

我知道這不是一個真正的解決方案,而是一種「解決方法」。然而,它的工作原理並使您的應用程序比擁有這輛越野車的UISearBar更漂亮。

3

對於我來說,閃爍的searchBar是由於在searchBar設置過程中沒有設置backgroundImage造成的。

斯威夫特:

searchBar.backgroundImage = UIImage() 
+0

嘿,這個工程! –

相關問題