xcode 如何仅为一个视图控制器设置 UINavigation 栏的 titletextattributes?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12809024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:45:30  来源:igfitidea点击:

How can I set the titletextattributes for a UINavigation bar for just one view controller?

iosxcodeuinavigationcontrolleruinavigationbarviewdidload

提问by Hudson Buddy

I'm trying to change the font size of just one of my view controllers because it lends itself to more text, thus requiring smaller font size than my other views. I am setting the navigation bar attributes in the appdelegate during didFinishLaunchingWithOptions, and then setting the font for my view controller in question during the viewDidLoad. However, when I go back a screen, or forward, the changes I made to the navigation bar are retained, i.e. the smaller font. Is there a way around this? For example setting the font back to normal upon exiting the view or something?

我正在尝试仅更改我的一个视图控制器的字体大小,因为它适用于更多文本,因此需要比我的其他视图更小的字体大小。我在 didFinishLaunchingWithOptions 期间在 appdelegate 中设置导航栏属性,然后在 viewDidLoad 期间为我的视图控制器设置字体。但是,当我返回屏幕或前进时,我对导航栏所做的更改会保留,即较小的字体。有没有解决的办法?例如在退出视图时将字体设置回正常还是什么?

Cliffsnotes: Trying to set font in just one view controller, but once I set it, it applies to all view controllers.

Cliffsnotes:试图在一个视图控制器中设置字体,但是一旦我设置了它,它就会应用于所有视图控制器。

Code is as follows:

代码如下:

In AppDelegate:

在 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]];

In the view controller viewDidLoad:

在视图控制器 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]];

采纳答案by rmaddy

The navbar is shared by all view controllers in the navigation controller. So once you change the navbar, all view controllers in the same nav stack will be affected. One option would be to set a custom titleView for this one view controller.

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

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

This way, only the one view controller shows the custom title.

这样,只有一个视图控制器显示自定义标题。

Another option would be to change the navbar's appearance in the view controller's viewWillAppear method and then reset it in the view controller's viewWillDisappear method. But this approach may not be as smooth as just using a custom titleView.

另一种选择是在视图控制器的 viewWillAppear 方法中更改导航栏的外观,然后在视图控制器的 viewWillDisappear 方法中重置它。但是这种方式可能不如使用自定义的titleView那么流畅。

回答by Aleksandr Osnach

So, now yours attributed keys is deprecated. Use this keys:

所以,现在你的属性键已被弃用。使用这个键:

NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
 [UIColor clearColor], NSForegroundColorAttributeName,
 [UIColor whiteColor], NSShadowAttributeName,
 [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], NSShadowAttributeName,
 [UIFont fontWithName:@"QuicksandBold-Regular" size:24.0], NSFontAttributeName,
 nil];