Prabhu
Prabhu

Reputation: 13355

Add navigation bar to a view controller without a navigation controller

How do you add a navigation bar to a view controller (collection view controller, actually) that is not embedded in a navigation controller? I tried dragging a Navigation Bar onto the view, but it's just not sticking. This is in Swift.

Upvotes: 11

Views: 18344

Answers (3)

david euler
david euler

Reputation: 824

Here is version for swift 5.

class ViewController: UIViewController, UINavigationBarDelegate {


override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(toolbar)
    toolbar.delegate = self
    
    let height: CGFloat = 75
    let navbar = UINavigationBar(frame: CGRect(x: 20, y: 20, width: UIScreen.main.bounds.width, height: height))
    navbar.backgroundColor = UIColor.white
    navbar.delegate = self

    let navItem = UINavigationItem()
    navItem.title = "Title"
    navItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .plain, target: self, action: nil)
    navItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .plain, target: self, action: nil)

    navbar.items = [navItem]

    view.addSubview(navbar)

    self.view.frame = CGRect(x: 0, y: height, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height - height))
    
}

}

Upvotes: 3

ICantSeeSharp
ICantSeeSharp

Reputation: 286

Updated answer for Swift 4:

private func addNavigationBar() {
    let height: CGFloat = 75
    var statusBarHeight: CGFloat = 0
    if #available(iOS 13.0, *) {
        statusBarHeight = view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
    } else {
        statusBarHeight = UIApplication.shared.statusBarFrame.height
    }
    let navbar = UINavigationBar(frame: CGRect(x: 0, y: statusBarHeight, width: UIScreen.main.bounds.width, height: height))
    navbar.backgroundColor = UIColor.white
    navbar.delegate = self as? UINavigationBarDelegate

    let navItem = UINavigationItem()
    navItem.title = "Sensor Data"
    navItem.leftBarButtonItem = UIBarButtonItem(title: "Back", style: .plain, target: self, action: #selector(dismissViewController))

    navbar.items = [navItem]

    view.addSubview(navbar)

    self.view?.frame = CGRect(x: 0, y: height, width: UIScreen.main.bounds.width, height: (UIScreen.main.bounds.height - height))
}

Upvotes: 1

Ike10
Ike10

Reputation: 1595

Try putting this code in your viewDidLoad:

let height: CGFloat = 75
let navbar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: height))
navbar.backgroundColor = UIColor.whiteColor()
navbar.delegate = self

let navItem = UINavigationItem()
navItem.title = "Title"
navItem.leftBarButtonItem = UIBarButtonItem(title: "Left Button", style: .Plain, target: self, action: nil)
navItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button", style: .Plain, target: self, action: nil)

navbar.items = [navItem]

view.addSubview(navbar)

collectionView?.frame = CGRect(x: 0, y: height, width: UIScreen.mainScreen().bounds.width, height: (UIScreen.mainScreen().bounds.height - height))

height can, of course, be anything you want. And the actions for the UIBarButtons are selectors to whatever function you want. (You also don't need to have buttons at all).

Edits:

  1. Adjusted the collectionView's frame so it would not overlap with UINavigationBar.
  2. Made height a constant so all its references can be changed in one place.

Upvotes: 10

Related Questions