gpt4 book ai didi

ios - 如何仅为一个 View Controller 设置导航栏的 titletext 属性?

转载 作者:可可西里 更新时间:2023-11-01 05:46:12 25 4
gpt4 key购买 nike

我正在尝试更改我的一个 View Controller 的字体大小,因为它适合更多文本,因此需要比我的其他 View 更小的字体大小。我在 didFinishLaunchingWithOptions 期间在 appdelegate 中设置导航栏属性,然后在 viewDidLoad 期间为我的相关 View Controller 设置字体。但是,当我后退或前进屏幕时,我对导航栏所做的更改将被保留,即较小的字体。有没有解决的办法?例如在退出 View 时将字体设置回正常?

Cliffsnotes:尝试只在一个 View Controller 中设置字体,但一旦我设置它,它就会应用于所有 View Controller 。

代码如下:

在 AppDelegate 中:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor clearColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"QuicksandBold-Regular" size:24.0],
UITextAttributeFont,
nil]];

在 View Controller viewDidLoad中:

[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor clearColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"QuicksandBold-Regular" size:18.0],
UITextAttributeFont,
nil]];

最佳答案

导航栏由导航 Controller 中的所有 View Controller 共享。因此,一旦更改导航栏,同一导航堆栈中的所有 View Controller 都会受到影响。一种选择是为这个 View Controller 设置自定义 titleView。

self.navigationItem.titleView = ... // some UILabel with the desired formatting

这样,只有一个 View Controller 显示自定义标题。

另一种选择是在 View Controller 的 viewWillAppear 方法中更改导航栏的外观,然后在 View Controller 的 viewWillDisappear 方法中重置它。但是这种方法可能不像使用自定义 titleView 那样顺利。

关于ios - 如何仅为一个 View Controller 设置导航栏的 titletext 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12809024/

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