【问题标题】:Why does the UISearchBar appear to have a strange flash when navigating back?为什么 UISearchBar 在导航返回时会出现奇怪的闪烁?
【发布时间】:2015-07-10 17:19:07
【问题描述】:

我的 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

【问题讨论】:

    标签: objective-c uisearchbar uisearchcontroller


    【解决方案1】:

    我遇到了同样的问题,我通过两种方式解决了:

    首先,你可以把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
    

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

    希望对你有帮助

    【讨论】:

    • 谢谢,@Omzarzi!我的关键是这个:_searchController.searchBar.barTintColor= [HN_APP_DELEGATE brandColor];
    【解决方案2】:

    对我来说,searchBar 闪烁的情况是由于在 searchBar 设置期间未设置 backgroundImage 造成的。

    斯威夫特:

    searchBar.backgroundImage = UIImage()
    

    【讨论】:

    • 谢谢!你节省了我的时间!。
    【解决方案3】:

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

    非常简单,用 Swift 3.0 编写。

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

    override func viewWillDisappear(_ animated: Bool) {
    
        searchBars.isHidden = true
    
    }
    

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

    override func viewDidAppear(_ animated: Bool) {
    
        self.searchBars.isHidden = false
    
    }
    

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

    【讨论】:

      猜你喜欢
      • 2018-05-25
      • 1970-01-01
      • 2020-10-12
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 2010-10-12
      • 2013-07-09
      相关资源
      最近更新 更多