Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation bar becomes white when a UISearchController is added to it

Tags:

ios

swift

ios13

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 clicks on the search bar. This happened since ios 13.1. This video shows the behaviour:

https://imgur.com/wn5zbnJ

My code consists of a simple storyboard with a NavigationController + a TableViewController, and the NavigationController has a color assigned to it: enter image description here

The ViewController consists of the following code:

class ViewController: UITableViewController {

    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        super.viewDidLoad()

        searchController.hidesNavigationBarDuringPresentation = false
        searchController.obscuresBackgroundDuringPresentation = false
        navigationItem.searchController = searchController
    }
}

I also added these keys to the info.plist file to force the app into light-mode, but if I remove these the same behaviour is still present:

<key>UIUserInterfaceStyle</key>
<string>Light</string>

This was tested on an iPhone XS Max, running iOS 13.1 beta 1. Is this expected behaviour or a bug which needs to be fixed?

like image 528
Devxln Avatar asked Sep 01 '19 13:09

Devxln


People also ask

How do I color a navigation bar in Swift?

navigationBar. barTintColor = UIColor. newBlueColor() and of course this just changes the colour of the navigation bar of the view controller that the code is within.


2 Answers

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
like image 178
mmklug Avatar answered Oct 29 '22 01:10

mmklug


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.

like image 39
Scott Ahten Avatar answered Oct 28 '22 23:10

Scott Ahten