1

I am not using a navigation controller control this view because the navigation controller forces me to use segues that I do not want to use (and I'm not sure how to over write those segues, I've tried multiple things but they always lead to a crash); although, the good thing about using the navigation controller, among many other things, is that there is no space between the navigation bar and the top of the phone.

However, because I want to use my own custom segues, I just added a navigation bar to my view controller. The problem this caused is as follows:

enter image description here

There is this space between the navigation bar and the top of the phone. And I do not want the space there. Anybody know how to fix this?

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
Jae Kim
  • 625
  • 3
  • 12
  • 23

2 Answers2

1

You can create navigation bar programatically this way:

// Create the navigation bar
let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account

navigationBar.barTintColor = UIColor.whiteColor()

// Create a navigation item with a title
let navigationItem = UINavigationItem()
navigationItem.title = "Profile"

// Assign the navigation item to the navigation bar
navigationBar.items = [navigationItem]

// Make the navigation bar a subview of the current view controller
self.view.addSubview(navigationBar)

Result:

enter image description here

Space is not there anymore.

Original Post: Adding Navigation Bar programmatically iOS

UPDATE:

If you want to add buttons add this code:

// Create left and right button for navigation item
let leftButton =  UIBarButtonItem(title: "LeftButton", style:   UIBarButtonItemStyle.Plain, target: self, action: "leftClicked:")
let rightButton = UIBarButtonItem(title: "RightButton", style: UIBarButtonItemStyle.Plain, target: self, action: "rightClicked:")

// Create two buttons for the navigation item
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton

And here is helper method:

func leftClicked(sender: UIBarButtonItem) {
    // Do something
    println("Left Button Clicked")
}

func rightClicked(sender: UIBarButtonItem) {
    // Do something
    println("Right Button Clicked")
}

And final code will be:

import UIKit

class ViewController: UIViewController {


    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.sharedApplication().statusBarStyle = .LightContent
        // Create the navigation bar
        let navigationBar = UINavigationBar(frame: CGRectMake(0, 20, self.view.frame.size.width, 44)) // Offset by 20 pixels vertically to take the status bar into account

        navigationBar.barTintColor = UIColor.whiteColor()
        // Create a navigation item with a title
        let navigationItem = UINavigationItem()
        navigationItem.title = "Profile"

        // Create left and right button for navigation item
        let leftButton =  UIBarButtonItem(title: "LeftButton", style:   UIBarButtonItemStyle.Plain, target: self, action: "leftClicked:")
        let rightButton = UIBarButtonItem(title: "RightButton", style: UIBarButtonItemStyle.Plain, target: self, action: "rightClicked:")

        // Create two buttons for the navigation item
        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton

        // Assign the navigation item to the navigation bar
        navigationBar.items = [navigationItem]

        // Make the navigation bar a subview of the current view controller
        self.view.addSubview(navigationBar)
    }

    func leftClicked(sender: UIBarButtonItem) {
        // Do something
        println("Left Button Clicked")
    }

    func rightClicked(sender: UIBarButtonItem) {
        // Do something
        println("Right Button Clicked")
    }

}
Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
0

First of all, if you are using standalone UINavigationBar just because you could not work out how segue works, my first advise would be to use UINavigationController as it gives you so much for free.

Per Apple Docs:

When you use a navigation bar as a standalone object, you are responsible for providing its contents. Unlike other types of views, you do not add subviews to a navigation bar directly. Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. A navigation item has properties for specifying views on the left, right, and center of the navigation bar and for specifying a custom prompt string.

If you still want to explore this path then you need to implement -positionForBar: of the UINavigationBarDelegate like this:

func positionForBar(bar: UIBarPositioning) -> UIBarPosition {
  return .TopAttached
}

You might have to set the tintColor as well to make it look aligned with status bar:

let viewWidth = self.view.frame.size.width
var navBar: UINavigationBar = UINavigationBar(frame: CGRect(x:0, y:20, width: viewWidth, height:44))
navBar.barTintColor = UIColor .lightGrayColor()
navBar.delegate = self
self.view.addSubview(navBar)
Abhinav
  • 37,684
  • 43
  • 191
  • 309