ios - 为什么 UINavigation 中的 UILabel 不居中

标签 ios iphone objective-c

我在导航 Controller 中使用自定义返回按钮:

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                             initWithTitle:NSLocalizedString(@"Back", nil)
                                             style:UIBarButtonItemStylePlain
                                             target:nil
                                             action:nil];
    //and add custom label in this:
    heightNav = self.navigationController.navigationBar.frame.size.height;
    widthNav  = self.navigationController.navigationBar.frame.size.width;
    self.aCNavLabel = [[ACNavLabel alloc] initWithFrame:CGRectMake(0, 0, widthNav, heightNav)];
    self.aCNavLabel.text = NSLocalizedString(@"Settings", nil);
    //
    UIView *titleNavView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, widthNav, heightNav)];
    self.navigationItem.titleView = titleNavView;
    [self.navigationItem.titleView addSubview:self.aCNavLabel];

但是标签没有居中。 请告诉我哪里出错了?

和自定义标签aCNavLabel: @implementation ACNavLabel

- (id)initWithFrame:(CGRect)frame
{
   self = [super initWithFrame:frame];
   if (self) {

      self.backgroundColor = [UIColor clearColor];
      self.textColor = [UIColor blackColor];
      self.font = FONT_OP_STD(22.0f);
 //   self.text = title;
      self.textAlignment = NSTextAlignmentCenter;
    }
    return self;
}

我为此更改了一些规则 self.navigationItem.titleView = self.aCNavLabel; 并且有一些不可思议的事情 enter image description here

这是第一个 viewController 和 enter image description here

接下来这是什么???

最佳答案

self.navigationController.navigationBar setTitleTextAttributes:(NSDictionary *)

这个方法就可以了 可以自定义navigationBar标题的字体、颜色、textAlign等属性;

关于ios - 为什么 UINavigation 中的 UILabel 不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20675378/

相关文章:

iphone - 核心数据 - 基本问题

ios - 在标签上显示字符串和变量

ios - 如何从 NSURLSession 中停止任务或部分

ios - 在根导航 Controller 之前展开模态 Segue - iOS

ios - 我可以将 iOS 应用程序启动到特定位置吗?

objective-c - 直接释放一个对象

iphone - AFNetworking:返回 200 且无内容时运行失败 block

ios - 无法满足协议(protocol)(具有关联类型)的一致性

iOS kontakt.io 信标搜索

iphone - 使用预编译器定义区分 iPhone 和 Apple TV