0

I make a navigation bar programmatically without a navigation controller. The problem is when I add a navigation item, they are displayed very high. Help solve. The screenshot shows how it looks

 override func viewDidLoad() {
    super.viewDidLoad()

    let bar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 100))
    //bar.setBackgroundImage(UIImage(), for: .default)
   // bar.shadowImage = UIImage()
    bar.backgroundColor = .black

    bar.isTranslucent = true
    view.addSubview(bar)

    let navItem = UINavigationItem(title: "Test")
    let menuButton = UIButton(type: .system)
    menuButton.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
    menuButton.addTarget(self, action: #selector(openSearch), for: .touchUpInside)
    menuButton.backgroundColor = .blue
    menuButton.setImage(UIImage(named: "icon_search"), for: .normal)
    let menuBarButtonItem = UIBarButtonItem(customView: menuButton)
    navItem.leftBarButtonItems = [menuBarButtonItem]
    bar.items = [navItem]
}

enter image description here

Mospy
  • 31
  • 9
  • The problem is that that is not how to make a “loose” navigation bar. If you don’t know how to do it, why do it? Wrap this whole thing in a navigation controller just to get a proper navigation bar, even if you don’t intend to push anything. – matt May 28 '20 at 02:14
  • Well, I've told you how to do it correctly, so do it that way and the navigation bar will behave the same way it does in a navigation controller. – matt May 28 '20 at 13:10
  • @matt I just need to have a timer in the navigation bar and when switching to other view controllers, there should be one timer. I was thinking of making a single navigation bar for all View Controllers, but if I make a navigation controller for each view controller, then I will have to transfer the timer to all View Controllers as well. Any ideas how to do this? – Mospy May 28 '20 at 13:25

0 Answers0