Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

ios - Navigation bar becomes white when a UISearchController is added to it

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...