gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 06:44:23 24 4
gpt4 key购买 nike

我到处查看,似乎找不到如何更改导航栏后退按钮文本的粗细。我想让它更薄。

此外,当我们谈论粗细时,您会如何改变导航栏的标题粗细?

谢谢!

最佳答案

您可以通过调整导航栏和后退按钮项上的titleTextAttributes来修改字体样式。

默认样式(iOS 8)

enter image description here

自适应尺寸和厚度(iOS < 8.2)

在 iOS 8.2 之前,调整字体粗细的唯一方法是选择其变体之一,例如 HelveticaNeue-LightHelveticaNeue-Bold 或使用

UIFont.boldSystemFontOfSize(_ fontSize: CGFloat)

但是,没有等效的 lightSystemFontOfSize 工厂函数。为了减少后退按钮和标题文本的重量和大小,您可以使用类似这样的东西(在呈现 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)

产生以下样式

enter image description here

自适应尺寸和厚度(iOS >= 8.2)

从 iOS 8.2 版本开始,您可以使用权重属性初始化系统字体。

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

可以在 Apple's official documentation 中找到一组预定义的权重常量。 .

在前面的例子的基础上你可以做这样的事情

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)]

最终会产生这样的样式

enter image description here

关于ios - 更改导航栏后退按钮的厚度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31999533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com