Reputation: 207
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
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? :/
Upvotes: 17
Views: 36015
Reputation: 1125
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".
Upvotes: 0
Reputation: 874
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
Upvotes: 34
Reputation: 993
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
Upvotes: 2
Reputation: 45
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://stackoverflow.com/a/25167491/2070902
Upvotes: 1