简体   繁体   English

更改导航栏后退按钮的厚度

[英]Changing Navigation Bar Back Button thickness

I looked everywhere and can't seem to find how to change the thickness of the navigation bar's back button text. 我四处张望,似乎找不到改变导航栏后退按钮文本粗细的方法。 I want it to be thinner. 我希望它变薄。

Also, while we are talking about thickness, how would you go about changing the navigation bar's title thickness as well? 另外,在我们讨论厚度时,您将如何更改导航栏的标题厚度?

Thank you! 谢谢!

You can modify the font style by adjusting the titleTextAttributes on the navigation bar and the back-button item. 您可以通过调整导航栏和后退按钮项上的titleTextAttributes来修改字体样式。

Default Style (iOS 8) 默认样式(iOS 8)

在此处输入图片说明

Adapting Size and Thickness (iOS < 8.2) 调整大小和厚度(iOS <8.2)

Prior to iOS version 8.2 the only way to adjust the weight of a font is to choose one of its variants like HelveticaNeue-Light or HelveticaNeue-Bold or by using 在iOS版本8.2之前,调整字体粗细的唯一方法是选择其变体之一,例如HelveticaNeue-LightHelveticaNeue-Bold或通过使用

UIFont.boldSystemFontOfSize(_ fontSize: CGFloat)

There is, however, no equivalent lightSystemFontOfSize factory function. 但是,没有等效的lightSystemFontOfSize工厂功能。 To reduce the weight and size of the back button and title text you could use something like this (inside a presenting view controller) 为了减少后退按钮和标题文本的重量和大小,您可以使用类似的方法(在呈现视图控制器内部)

let rootController = UIViewController()
rootController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back",
    style: .Plain, target: nil, action: nil)
rootController.navigationItem.backBarButtonItem?.setTitleTextAttributes(
    [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 15)!],
    forState: .Normal)

let detailController = UIViewController()
detailController.title = "Title"

let navController = UINavigationController()
navController.viewControllers = [rootController, detailController]
navController.navigationBar.titleTextAttributes = [
    NSFontAttributeName: UIFont(name: "HelveticaNeue", size: 18)!]

presentViewController(navController, animated: true, completion: nil)

which produces the following style 产生以下样式

在此处输入图片说明

Adapting Size and Thickness (iOS >= 8.2) 调整大小和厚度(iOS> = 8.2)

Since iOS version 8.2 you can initialize system fonts with a weight attribute. 从iOS 8.2版本开始,您可以使用weight属性初始化系统字体。

UIFont.systemFontOfSize(_ fontSize: CGFloat, weight weight: CGFloat)

There is a set of predefined weight constants that can be found in Apple's official documentation . Apple的官方文档中可以找到一组预定义的权重常数。

Building upon the previous example you could do something like this 在前面的示例的基础上,您可以执行以下操作

let rootController = UIViewController()
rootController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "Back",
    style: .Plain, target: nil, action: nil)
rootController.navigationItem.backBarButtonItem?.setTitleTextAttributes(
    [NSFontAttributeName: UIFont.systemFontOfSize(15, weight: UIFontWeightThin)],
    forState: .Normal)

let detailController = UIViewController()
detailController.title = "Title"

let navController = UINavigationController()
navController.viewControllers = [rootController, detailController]
navController.navigationBar.titleTextAttributes = [
    NSFontAttributeName: UIFont.systemFontOfSize(18, weight: UIFontWeightLight)]

which ultimately results in a style like this 最终导致这样的风格

在此处输入图片说明

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

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