简体   繁体   English

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

[英]How to change navigation item font size for iPad?

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 我能够在情节提要中更改导航项的标题字体,但没有看到任何选项来更改导航项的标题字体以适应不同的宽度,因此我在viewDidLoad()进行了此操作以更改字体大小

   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? 我无法更改常规宽度的设备的字体,我缺少什么?

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

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 我认为UI_USER_INTERFACE_IDIOM()是目标C。您应该使用类似的方法快速检查设备习惯用法

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 这应该足以更改导航栏标题字体的大小

You can try that : Device.swift 您可以尝试: Device.swift

Use contains() to check that your device type name include "iPad". 使用contains()检查您的设备类型名称是否包含“ iPad”。 So, it will do for all iPad Devices 因此,它将适用于所有iPad设备

1. Drag UINavigationItem and put at your view controller navigation bar at storyboard. 1.拖动UINavigationItem并将其放在情节提要的视图控制器导航栏中。

2. Connect that UINavigationItem with the Outlet that is your code. 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
}

That's all I got for now. 这就是我现在所拥有的。 Other feedbacks are also welcome on customizing UINavigationItem. 在定制UINavigationItem方面也欢迎其他反馈。 If I can find new one which is better than this, I will update here. 如果我能找到比这更好的新书,我将在这里进行更新。 Good Luck. 祝好运。

Here is the result : 结果如下:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM