使用UIAppearance更改

使用UIAppearance更改

有什么方法可以使用UIAppearance更改UINavigationBar内部标签的高度。这是代码和正在发生的事情的图像,因此您可以了解问题所在。

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-5.0 forBarMetrics:UIBarMetricsLandscapePhone];

NSDictionary *textAttributes =  [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[UIFont fontWithName:@"MarketingScript" size:34.0], nil]
                                                            forKeys:[NSArray arrayWithObjects:UITextAttributeFont, nil]];

最佳答案

我最终通过创建自定义UIView解决了这个问题:

+(NavigationBarTitleLabel *)titleLabelWithTitle:(NSString *)title {
  // this will appear as the title in the navigation bar
  NavigationBarTitleLabel *labelWrapper = [[NavigationBarTitleLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 34)];
  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 8, 200, 32)];

  label.font            = [UIFont fontWithName:@"MarketingScript" size:28.0];
  label.text            = title;
  label.textColor       = XHEXCOLOR(0XFFFFFF);
  label.textAlignment   = UITextAlignmentCenter;
  label.backgroundColor = [UIColor clearColor];
  label.layer.shadowColor = [UIColor blackColor].CGColor;
  label.layer.shadowRadius = 2;
  [labelWrapper addSubview:label];
  [labelWrapper sizeToFit];

  return labelWrapper;
}

然后在navigationItem上设置titleView属性:
self.navigationItem.titleView = [NavigationBarTitleLabel titleLabelWithTitle:self.title];

关于iphone - 使用UIAppearance更改标签高度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7779190/

10-12 13:26