Navigation bar becomes white when a UISearchController is added to it

前端 未结 4 681
感情败类
感情败类 2020-12-14 23:50

When I add a UISearchController to a UINavigationItem from an UINavigationController; it becomes white when the view loads and changes to the color specified when the user c

相关标签:
4条回答
  • 2020-12-15 00:03

    I'm experiencing same issue, and already opened a radar to Apple. Even though, if you set the backgroundColor to the appearance of the UINavigationBar, then the navigation bar instead of becoming transparent when pulling down and showing the search bar becomes that color, but then the status bar still remains white.

    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().barTintColor = .blue
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    UINavigationBar.appearance().backgroundColor = .red
    

    Here is some code that tries to mitigate that behaviour, but I'm open to hear for the status bar thing

    0 讨论(0)
  • 2020-12-15 00:13

    This appears to be a bug in iOS 13.1. Specifically, there is a new iOS 13 specific appearance (UINavigationBarAppearance) for navigation bars which specifies the appearance when the scroll view is scrolled to the top, along with the default state. Normally changes like this only go into effect when the app is built with the corresponding SDK (iOS 13.1). However, there seems to be a bug where the behavior also occurs when an app is built using the iOS 12 SDK.

    See: https://developer.apple.com/documentation/uikit/uinavigationbarappearance

    Update: There is a workaround here: https://itnext.io/fixing-issues-caused-by-future-sdks-ae0896384abf

    Essentially, if your app is running on a device running iOS 13, it's possible to create instances of the new classes via NSClassFromString() in swift, then use a bit of objective-c runtime magic to configure the navigation bar.

    0 讨论(0)
  • 2020-12-15 00:17

    It looks like it is required to use the new UINavigationBarAppearance on iOS 13. Try to add this to your viewDidLoad:

    let appearance = UINavigationBarAppearance()
    appearance.backgroundColor = .systemRed
    appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    navigationItem.standardAppearance = appearance
    navigationItem.scrollEdgeAppearance = appearance
    

    You will probably also want to set the searchField backgroundColor:

    let searchField = searchController.searchBar.searchTextField
    searchField.backgroundColor = .systemBackground
    
    0 讨论(0)
  • 2020-12-15 00:17

    What you want was not clear in your question. If you want, however, add searchbar into navigationBar and with a specific color this might help you.

    The process of placing seachbar into navigation bar:

    let searchController = UISearchController(searchResultsController: nil)
    navigationItem.searchController = searchController
    

    You can add whatever controller you want into 'searchResultsController' value.

    If you want to set background color into a specific color, you can change the bar tint from Storyborad -> navigationbar -> navigation bar attiribute inspection.

    Also, this below code for the AppDelegate.swift file will do the same. 'tintcolor' and 'titletextcolor' is commented

      func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            // Override point for customization after application launch.
    
            let navigationBarAppearace = UINavigationBar.appearance()
            navigationBarAppearace.barTintColor = UIColor.blue
    
            // navigationBarAppearace.tintColor = UIColor.red
            //navigationBarAppearace.titleTextAttributes = [.foregroundColor: UIColor.red]
            return true
        }
    
    0 讨论(0)
提交回复
热议问题