Swift Navigation Bar Button and Title don't appear
Asked Answered
C

4

17

I'd like to create a Navigation bar on top of my App. I created an Navigation Controller -> Tab Bar Controller -> Navigation Controller -> Table Controller

  1. I dragged a Bar Button Item on the upper right side.
  2. I double clicked the title in the middle of the Table Controller and wrote a text.
  3. I also tried it with this code in the viewDidLoad() of my controller:

    self.navigationController.navigationBar.topItem.title = "some title"
    self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)
    self.navigationItem.title = "YourTitle"
    

Non of it worked - what am I doing wrong? :/

Compendious answered 20/8, 2014 at 9:30 Comment(1)
Did you find a solution?Doit
H
34

You can try something like this:

self.title = "Your Title"

var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")

var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton
Hereld answered 24/10, 2014 at 3:59 Comment(3)
How can I set an icon instead of text?Disintegrate
I found how:) --> var homeBytton : UIBarButtonItem = UIBarButtonItem(image: <#UIImage?#>, style: <#UIBarButtonItemStyle#>, target: <#AnyObject?#>, action: <#Selector#>)Disintegrate
Remember that you can leave out the UIBarButtonItemStyle and just use something like .Plain. ...also you should be using let instead of var.Altorilievo
L
2

I believe you are looking for a titleView.

Try:

var navBarTitleView = UIView(frame: CGRectMake(0.0, 0.0, self.view.frame.width, 44.0))
navBarTitleView.backgroundColor = UIColor.brownColor()
self.navigationItem.titleView = navBarTitleView
Lorenelorens answered 18/10, 2014 at 16:43 Comment(0)
W
1

I am pretty sure that you can just set the title of your table controller, in its viewDidLoad, which is the title displayed in your navigation bar.

this answer explains a slightly different question, but the concept is the same:

https://mcmap.net/q/117658/-changing-navigation-title-programmatically

Weathered answered 21/8, 2014 at 14:51 Comment(0)
D
0

if you want to set your "UINavigationBar" according to your wish Please follow this very safely ,its so simple in swift language ,hope it will help you a lot .

STEP 1:-

Create a category class in swift just go on "New file" and add "Simple swift class " and given it name "MyCustomNavBarCategory.swift"

STEP 2:-

Just "Clean the class (remove all code from this class)" and "Paste my give code below" :-

//class will Start from below line start copy from here

let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height

import UIKit

extension UINavigationBar {

//First increase navigation bar height .
override public func sizeThatFits(size: CGSize) -> CGSize {
    NSLog("%f", SCREEN_HEIGHT*0.06);

    let newSize:CGSize = CGSizeMake(self.frame.size.width, SCREEN_HEIGHT*0.10)
    return newSize;
}

//then set layOut of your Custom Nav for several objects like titleview,buttons and all
public override func layoutSubviews() {
   for var view in self.subviews
    {
        NSLog("view %@", view);
        let str :String = view.classForCoder.description();
       if(str == "UILabel")
        {
              //play with title view frame
            var f       = view.frame;
            f.origin.y  = self.frame.size.height/2 - view.bounds.size.height/2  ;
            view.frame  = f;
            view.backgroundColor = UIColor.greenColor();
        }
        else if(str == "UIButton")
        {

             //play with buttons of navigation bar  view frame
            var f = view.frame;
            f.origin.y = (self.frame.size.height/2)  - (view.bounds.size.height/2) ;
            view.frame = f;
            view.backgroundColor = UIColor.greenColor();
        }
    }
}

}

//class will end on above stop copy code on above line and just past this in to your "MyCustomNavBarCategory.swift". that's it enjoy ;).

STEP 3:- Now you can set any frame to any object of UINavigationBar in this class "It's very powerful category of UINavigationBar if you will implement it carefuly".

Duckboard answered 11/12, 2015 at 14:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.