Reputation: 13355
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
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
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
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 UIBarButton
s are selectors to whatever function you want. (You also don't need to have buttons at all).
Edits:
collectionView
's frame so it would not overlap with UINavigationBar
.Upvotes: 10