Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the font size of navigation bar title

I try to set the fontsize of my navigation title and other items. How can I do this ?

There are a lot of answers mentoining that we just can set this with the method uinavigationbar.setTitleTextAttributes, but this method doesn't exist (anymore)

Does someone know how to do ?

navigationBar = UINavigationBar(frame: CGRectMake(0, 0, fDialogWidth, navbarHeigth))
        let navigationItem = UINavigationItem()
        navigationItem.title = sTitle;
        // Create left and right button for navigation item
        let leftButton = UIBarButtonItem(title: sBacklabel, style: UIBarButtonItemStyle.Plain, target: self, action: "backAction:")
        let rightButton = UIBarButtonItem(title: saddBookmark, style: UIBarButtonItemStyle.Plain, target: self, action: "addBookmarkAction:")



        // Create two buttons for the navigation item
        navigationItem.leftBarButtonItem = leftButton
        navigationItem.rightBarButtonItem = rightButton
        navigationBar.items = [navigationItem];

        navigationBar.frame = CGRectMake(0, 0, screenwidth, navbarHeigth);
like image 264
mcfly soft Avatar asked Oct 30 '15 08:10

mcfly soft


People also ask

How do I change the Navigation Bar on my Iphone?

Change the Bar Style A user changes the navigation bar's style, or UIBarStyle , by tapping the “Style” button to the left of the main page. This button opens an action sheet where users can change the background's appearance to default, black-opaque, or black- translucent.

How do I change the Navigation Bar title color in Swift?

The title color of Navigation Bar can be changed in Storyboard. Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu. Save this answer.


3 Answers

Add these lines to your viewDidLoad() method:

self.navigationController?.navigationBar.topItem?.title = "Your title" 
self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "Your Font", size: 34)!, NSForegroundColorAttributeName: UIColor.whiteColor()]
like image 66
NSNoob Avatar answered Nov 15 '22 07:11

NSNoob


Update for swift 4

Presumably you want to set the tittle for UINavigationbar with system fontsize 30 and yellow color. The syntax would probably look like the followings:

navigationItem.title = "Your Title"    
navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 30),NSAttributedStringKey.foregroundColor: UIColor.yellow]
like image 44
PhDang Avatar answered Nov 15 '22 06:11

PhDang


try this...

 let titleAttributes = [
        NSFontAttributeName: UIFont(name: "your font name", size: 15)!
    ]
 self.navigationController?.navigationBar.titleTextAttributes = titleAttributes

If you are using system font then

 let titleAttributes = [
        NSFontAttributeName: UIFont.systemFontOfSize(15)
 ]
 self.navigationController?.navigationBar.titleTextAttributes = titleAttributes
like image 22
EI Captain v2.0 Avatar answered Nov 15 '22 08:11

EI Captain v2.0