UISearchController doesn't work properly with a non-translucent UINavigationBar

后端 未结 5 747
走了就别回头了
走了就别回头了 2020-12-16 11:16

Currently I am trying to embed a UISearchController into my application. But the UISearchBar, which is a property of the UISearchController, doesn\'t get displayed properly,

相关标签:
5条回答
  • 2020-12-16 11:45

    Ok, this one is a SUPER pain to debug but not that bad to fix. It's all down to the way Apple changed the appearance of navigation bars. It can be fixed by creating a UINavigationBarAppearance object, configuring it with the visual properties you want (i.e. background colour etc) and then assigning it to standardAppearance and scrollEdgeAppearance on UINavigationBar.appearance() - you can have two different instances with different settings if you want.

    A simple implementation might look like this:

    let appearance = UINavigationBarAppearance()
    appearance.configureWithDefaultBackground()
    appearance.backgroundColor = barColor
    appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: textColor]
    
    UINavigationBar.appearance().standardAppearance = appearance
    UINavigationBar.appearance().scrollEdgeAppearance = appearance
    

    (Naturally replace barColor and textColor with the colours of your choice!)

    0 讨论(0)
  • 2020-12-16 11:50

    if someone have a problem like non-translucent hidden the search bar u can just had this :

    self.definesPresentationContext = true

    Regards

    0 讨论(0)
  • 2020-12-16 12:01

    One workaround for this is to make the status bar translucent just before the search is going to become active, and remove the translucency when the search is about become inactive.

    You can do this by registering your view controller as a delegate of UISearchController, and implementing the willPresentSearchController and willDismissSearchController methods. For example (in Swift):

    Declare your view controller as a delegate of UISearchController:

     class MyViewController: UITableViewController, UISearchControllerDelegate
    

    Don't forget to actually set it as the delegate, for instance in viewDidLoad add:

        searchController.delegate = self
    

    And finally:

    func willPresentSearchController(searchController: UISearchController) {
        navigationController?.navigationBar.translucent = true
    }
    
    func willDismissSearchController(searchController: UISearchController) {
        navigationController?.navigationBar.translucent = false
    }
    
    0 讨论(0)
  • 2020-12-16 12:03

    All I needed was:

    func viewDidLoad() { 
    
        extendedLayoutIncludesOpaqueBars = true
    }
    
    0 讨论(0)
  • 2020-12-16 12:04

    It's clearly a bug (rdar://20942583).

    My workaround is to set

    self.edgesForExtendedLayout = UIRectEdgeAll;
    self.extendedLayoutIncludesOpaqueBars = YES;
    

    This allows you to keep the navigation bar opaque. The downside is that the content flows below the bar even if it can't be seen, creating some overhead.

    0 讨论(0)
提交回复
热议问题