繁体   English   中英

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

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

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

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

}

我无法更改常规宽度的设备的字体,我缺少什么?

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

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

我认为UI_USER_INTERFACE_IDIOM()是目标C。您应该使用类似的方法快速检查设备习惯用法

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

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

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 5.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方面也欢迎其他反馈。 如果我能找到比这更好的新书,我将在这里进行更新。 祝好运。

结果如下:

在此处输入图片说明

暂无
暂无

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

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