Search code examples
iosswiftuinavigationcontrolleruinavigationbar

Add UIView in navigation bar


I want to create UINavigationBar with rounded corner. It will look like this rounded navigation bar

What I am thinking is I add UIView with rounded corner and insert it in navigation bar. So this is my code

let roundView = UIView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44))
roundView.backgroundColor = UIColor.whiteBackground
roundView.roundCorners(corners: [.topLeft, .topRight], radius: 20)
navigationController?.navigationBar.insertSubview(roundView, at: 0)
setTitleTextColor(color: UIColor.black)

By the UI, this works well. But then my UIBarButtonItem is missing, it covered up by my custom view and couldn't be clicked. So my question is, how to add subview in navigation bar?

Thank you!


Solution

  • Just not use UINavigation bar and create all by scratch. Is the easiest way. Otherwise you can try with pattern image:

    navigationController?.navigationBar.backgroundColor = UIColor(patternImage: UIImage(named: "background.png"))