我在导航控制器中使用自定义后退btn:
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; 并且有一些令人难以置信的东西
这是第一个viewController和
接下来这是什么????
答案 0 :(得分:1)
self.navigationController.navigationBar setTitleTextAttributes:(NSDictionary *)
这个方法会好的 您可以为客户提供有关字体,颜色,文本对齐和其他属性的导航栏标题;
答案 1 :(得分:0)
您需要将文本对齐设置为居中,如:
aCNavLabel.textAlignment = NSTextAlignmentCenter;
有关更多选项,请参阅UILabel textAlignment property。
或@NANNAV建议使用:
self.navigationItem.titleView = self.aCNavLabel;
答案 2 :(得分:0)
self.aCNavLabel = [[ACNavLabel alloc] initWithFrame:CGRectMake(0, 0, widthNav, heightNav)];
self.aCNavLabel.text = NSLocalizedString(@"Settings", nil);
self.aCNavLabel.textAlignment = NSTextAlignmentCenter; // SET THIS
答案 3 :(得分:0)
aCNavLabel.textAlignment = NSTextAlignmentCenter;
试一试。
答案 4 :(得分:0)
为UINavigationBar创建类别,并以编程方式调整标题标签的位置。将ACNavLabel视图设置为titleview。
UINavigationBar的+ MyNavigationBar.h
@interface UINavigationBar (MyNavigationBar)
@end
UINavigationBar的+ MyNavigationBar.m
@implementation UINavigationBar (MYNavigationBar)
- (void)layoutSubviews {
for (UIView *view in self.subviews) {
if ([view isKindOfClass:[ACNavLabel class]]) {
view.center = CGPointMake(self.center.x, self.center.y);
}
}
}
@end