如何更改iPad的导航项字体大小?

时间:2016-12-20 00:41:19

标签: ios swift

我能够在故事板中更改导航项的标题字体,但我没有看到任何更改不同宽度的导航项标题字体的选项,所以我在viewDidLoad()中更改了字体的大小

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

}

我错过了什么,因为我无法更改常规宽度设备的字体?

3 个答案:

答案 0 :(得分: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
    }
}

答案 1 :(得分:0)

首先,您确定您的设备检查工作正常吗?

我认为UI_USER_INTERFACE_IDIOM()是目标C.你应该使用这样的东西来检查swift中的设备习语

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

然后像这样修改标题字体大小

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

这足以改变导航栏标题字体大小的大小

答案 2 :(得分:0)

您可以尝试:Device.swift

使用contains()检查您的设备类型名称是否包含" iPad"。因此,它适用于所有iPad设备

<强> 1。拖动UINavigationItem并将其放在故事板上的视图控制器导航栏中。

<强> 2。将UINavigationItem与作为代码的Outlet连接。

@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
}

这就是我现在所得到的一切。其他反馈也欢迎定制UINavigationItem。如果我能找到比这更好的新的,我会在这里更新。祝你好运。

结果如下:

enter image description here