简体   繁体   English

UINavigationBar标题对齐问题

[英]Issue with UINavigationBar title alignment

I have an issue changing the font for the Title in my navigation controller. 我在导航控制器中更改标题的字体时遇到问题。

Here is my code (from my subclass of UINavigationController): 这是我的代码(来自我的UINavigationController的子类):

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary * attributes = @{UITextAttributeTextColor  :[UIColor whiteColor],
                                  UITextAttributeFont       :[UIFont fontWithName:@"Blanch" size:50],
                                  };
    self.navigationBar.titleTextAttributes  = attributes;
    CGFloat verticalOffset = -8;
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset forBarMetrics:UIBarMetricsDefault];

    UIImage *image = [UIImage imageNamed:NAVBAR_IMAGE];
    [self.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}

This is what it looks like: 看起来是这样的:

在此处输入图片说明

If I comment out the line setting the font this is what it looks like (as expected with the vertical offset): 如果我注释掉设置字体的行,这就是它的样子(与垂直偏移一样):

在此处输入图片说明

The only reason the vertical offset is there is that I thought it might have solved the problem. 垂直偏移存在的唯一原因是我认为它可能已经解决了问题。 I don't need it and it is commented out now, but it does NOT change the output - it still looks the same just lower on the NavBar. 我不需要它,现在已将其注释掉,但是它不会更改输出-在NavBar上它看起来还是一样。

Any ideas how to solve this? 任何想法如何解决这个问题? I think it may be that the custom font has a lot of line-spacing which I guess could be the problem. 我认为自定义字体可能有很多行距,我想可能是问题所在。

I have been trying to find a way to change the origin/height/baseline alignment of the title but can't see a way to do any of these. 我一直在试图找到一种方法来更改标题的原点/高度/基线对齐,但是看不到任何一种方法来执行这些操作。 Any idea how to solve this problem ? 任何想法如何解决这个问题?

Create a container UIView which is the same width as your label, and height of UINavigationBar (around 46 pixels?). 创建一个容器UIView ,该容器的宽度与标签的宽度相同,并且与UINavigationBar高度相同(约46像素?)。 Set clipsToBounds on this container view to YES. 将此容器视图上的clipsToBounds设置为YES。 Now add your label to this container view, and make the container view the titleView. 现在,将标签添加到此容器视图,并使该容器视图成为titleView。

Setting Navigationbar image for ios5 设置ios5的Navigationbar图像

if ([self.navigationController.navigationBar respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];
}

Setting Navigationbar image for ios6 设置ios6的Navigationbar图像

if([[UINavigationBar appearance] respondsToSelector:@selector( setBackgroundImage:forBarMetrics:)]){
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"YOUR_NAVIGATION_BAR_IMAGE"] forBarMetrics:UIBarMetricsDefault];

after than create label in center on your navigation bar center with your title. 然后,在标题上的导航栏中心创建标签。

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

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