0

I am able to change title font of Navigation Item in the storyboard but I did not see any option to change title font of navigation item for different widths so I did this in my viewDidLoad() to change the size of font

   if (UI_USER_INTERFACE_IDIOM() == .pad)
        {
      self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 54)!, NSForegroundColorAttributeName: UIColor.black]

}

What am I missing as I am not able to change font for regular width devices?

Coder221
  • 1,293
  • 2
  • 15
  • 35
  • Why are you checking the interface idiom? Things should be done based on size, not a specific device. – rmaddy Dec 20 '16 at 00:51
  • 1
    And please explain your issue. You haven't stated what the problem is. Is the `if` statement true? Are you sure that `self.navigationController` isn't `nil`? – rmaddy Dec 20 '16 at 00:52
  • I did that because, I did not find Title Font for regular width size in storyboard interface. – Coder221 Dec 20 '16 at 00:53
  • Create a UILabel object. Set an attributed string to that UILabel object. Then add the label object to the navigation controller. There might be an easier approach, but it at least works for me. – El Tomato Dec 20 '16 at 02:31

3 Answers3

0
override func viewDidLoad() {
    super.viewDidLoad()

    if (UI_USER_INTERFACE_IDIOM() == .pad){
        let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 40))
        titleLabel.font = UIFont.systemFont(ofSize: 24);
        titleLabel.text = "Check my size"
        self.navigationItem.titleView = titleLabel
    }
}
Yun CHEN
  • 5,785
  • 3
  • 24
  • 31
  • @Coder221, I have tested this in a brand new project, a UIViewController with UINavigationController, it works. Did you test it in iPhone? If yes, you need to remove "UI_USER_INTERFACE_IDIOM() == .pad". If your viewController doesn't belong to a NavigationController, it will fail also. – Yun CHEN Dec 20 '16 at 02:22
  • @Coder221, another solution if you are using Storyboard: Just drag a UIView into navigation bar title area. And then, drag a label into that UIView, you will be able to set the font of the label. Good luck! – Yun CHEN Dec 20 '16 at 02:26
  • It doesn't work in iPhone as well. My NavigaitonController is in ViewController. – Coder221 Dec 20 '16 at 02:27
  • in iPhone, you need to remove "if (UI_USER_INTERFACE_IDIOM() == .pad){". – Yun CHEN Dec 20 '16 at 02:29
  • yes I did remove it. Still doesn't work. But I have no problem with phone – Coder221 Dec 20 '16 at 02:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130989/discussion-between-yun-chen-and-coder221). – Yun CHEN Dec 20 '16 at 02:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131003/discussion-between-yun-chen-and-coder221). – Yun CHEN Dec 20 '16 at 06:02
0

First are you sure your device check is working?

I think UI_USER_INTERFACE_IDIOM() is Objective C. You should be using something like this to check the device idiom in swift

UIDevice.currentDevice().userInterfaceIdiom == .pad
UIDevice.currentDevice().userInterfaceIdiom == .phone
UIDevice.currentDevice().userInterfaceIdiom == .unspecified

Then modify the title font size like this

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 5.0)]

This should be enough to change the size of the nav bar title font size

Pato Salazar
  • 1,307
  • 11
  • 20
  • Yes my check is working but the code you provided is not, also the code you provided is same as I used. – Coder221 Dec 20 '16 at 03:13
  • swift 3.0?... Check this answer http://stackoverflow.com/questions/24059327/detect-current-device-with-ui-user-interface-idiom-in-swift – Pato Salazar Dec 20 '16 at 03:16
  • I don't have a problem with checking what device it is. – Coder221 Dec 20 '16 at 03:17
  • This is the funny thing... the code you provided it is actually working for me. Could you share the class itself where this code lives? – Pato Salazar Dec 20 '16 at 03:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130993/discussion-between-coder221-and-pato-salazar). – Coder221 Dec 20 '16 at 03:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131002/discussion-between-coder221-and-pato-salazar). – Coder221 Dec 20 '16 at 05:52
0

You can try that : Device.swift

Use contains() to check that your device type name include "iPad". So, it will do for all iPad Devices

1. Drag UINavigationItem and put at your view controller navigation bar at storyboard.

2. Connect that UINavigationItem with the Outlet that is your code.

@IBOutlet weak var rightNavItem : UINavigationItem!

override func viewDidLoad() {
    super.viewDidLoad()

    guard let navController = self.navigationController else{
        print("Navigation Controller is nil")
        return
    }

    navController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 25)!, NSForegroundColorAttributeName: UIColor.black]

    //This code is for rightbarbutton

    rightNavItem.rightBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 10)!, NSForegroundColorAttributeName: UIColor.blue], for:.normal)

    //.selected or .normal or .disable
}

That's all I got for now. Other feedbacks are also welcome on customizing UINavigationItem. If I can find new one which is better than this, I will update here. Good Luck.

Here is the result :

enter image description here

Thiha Aung
  • 4,656
  • 7
  • 34
  • 73