user6777252
user6777252

Reputation:

How to show the tittle in navigation bar programmatically in swift3

I just try to extend my height of navigation bar...I tried that code below, Its working fine....but it can't able to show the title? I tried self.title ="" and self.navigationitem.title ="" also......

self.navBar.tintColor = UIColor.blue
           // self.navigationItem.title = "Instrumental"
           self.title = "Instrumental"
            navBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
            let backButton = UIButton(type: .custom)
            backButton.frame = CGRect(x: 380, y: 15, width: 30, height: 30)
            backButton.setImage(UIImage(named:"vertical-dots (1)"), for: .normal)
          //  backButton.addTarget(self, action: #selector(ViewController.backButton(_:)), for: .TouchUpInside)
            navBar.addSubview(backButton)

func setNavBarToTheView() {
    self.navBar.frame = CGRect(x:0, y:0, width:420, height:60)  // Here you can set you Width and Height for your navBar
    self.navBar.backgroundColor = (UIColor.blue)
    self.view.addSubview(navBar)
}

enter image description here

Upvotes: 1

Views: 2950

Answers (2)

Anand Nimje
Anand Nimje

Reputation: 6251

You can add title programmatically for navigation bar like this : -

Swift 3

override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "First View"
 }

See below image

Upvotes: 3

rmaddy
rmaddy

Reputation: 318904

It appears that you are manually adding a UINavBar to the view. When you do that you lose all of the automatic features when you put your view controller into a navigation controller.

To set the title of your nav bar you need to also create a UINavigationItem and set it to the nav bar's items property.

Things really would be a lot simpler if you put your view controller in a navigation controller. Then you get all of the default behavior including a standard nav bar without the need to do all the work of adding your own.

Upvotes: 1

Related Questions