UINavigationItem titleView位置

问题描述 投票:15回答:7

我正在使用UINavigationItem的titleView属性来设置具有所需字体大小/颜色的自定义UILabel。这是我的代码:

self.headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 400.0, 44.0)];
self.headerLabel.textAlignment = UITextAlignmentCenter;
self.headerLabel.backgroundColor = [UIColor clearColor];
self.headerLabel.font = [UIFont boldSystemFontOfSize:20.0];
self.headerLabel.textColor = [UIColor colorWithRed:0.259 green:0.280 blue:0.312 alpha:1.0];
self.navigationItem.titleView = self.headerLabel;

在导航栏中,我还有一个左栏按钮。结果是:文本未正确居中。我尝试设置标签的x原点,但这无效。

iphone objective-c cocoa-touch ipad uinavigationitem
7个回答
15
投票

如果将headerLabel设置为titleView的子视图,则可以设置headerLabel的框架以控制它在titleView中的位置。

您现在的操作方式,您没有控制权。我认为操作系统会根据可用空间为您选择titleView的框架。

希望这会有所帮助!


19
投票

代替initWithFrame,只需使用init并将[self.headerLabel sizeToFit] after放在最后一行代码中。


3
投票

我在应用商店中拥有的每个应用中,我的导航栏都使用了自定义标题标签。我已经测试了许多不同的方法,并且通过far在导航栏中使用自定义标签的最简单方法是完全忽略titleView并将标签直接插入navigationController.view

通过这种方法,很容易使标题标签的框架始终与navigationBar的框架匹配-即使您使用的是非标准尺寸的自定义navBar。

- (void)viewDidLoad {
   [self.navigationController.view addSubview:self.titleLabel];
   [super viewDidLoad];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:
             (UIInterfaceOrientation)interfaceOrientation {
   return YES;
}

- (void)didRotateFromInterfaceOrientation:
             (UIInterfaceOrientation)fromInterfaceOrientation {
   [self frameTitleLabel];
}

- (UILabel *) titleLabel {
   if (!titleLabel) {
      titleLabel = [[UILabel alloc]           
          initWithFrame:self.navigationController.navigationBar.frame];

       titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
       titleLabel.text = NSLocalizedString(@"Custom Title", nil);
       titleLabel.textAlignment = UITextAlignmentCenter;
       titleLabel.lineBreakMode = UILineBreakModeTailTruncation;
   }
   return titleLabel;
}

- (void) frameTitleLabel {
   self.titleLabel.frame = self.navigationController.navigationBar.frame;
}

这种方法的一个警告是,如果您不小心将标题设置得太长,则标题可能会流过导航栏中任何按钮的顶部。但是,与IMO相比,它要解决的问题要少得多:1)带有rightBarButton时标题未正确居中,或2)带有leftBarButton时标题未出现。


3
投票

我有同样的问题;我只是以某种方式通过计算标题长度并相应地设置标签框架宽度来解决此问题。虽然这不是一个完美的方法,但是可以管理。这是代码。

label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [ UIFont fontWithName: @"XXII DIRTY-ARMY" size: 32.0 ];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.0f];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor orangeColor];
//label.text=categoryTitle;
CGFloat verticalOffset = 2; 
NSString *reqSysVer = @"5.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)           
{
    if (categoryTitle.length > 8)
    {
    label.frame = CGRectMake(0, 0, 300, 44);
    }else {
        label.frame = CGRectMake(0, 0, 80, 44);
    }

    self.navigationItem.titleView = label;  
     self.navigationItem.title=label.text;
    [[UINavigationBar appearance] setTitleVerticalPositionAdjustment:verticalOffset   forBarMetrics:UIBarMetricsDefault];
     [[UIBarButtonItem appearance] setTintColor:[UIColor newBrownLight]];


}

1
投票

只需计算所需的确切帧大小并向左对齐:

UIFont* font = [UIFont fontWithName:@"Bitsumishi" size:20];
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [title sizeWithFont:font constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeCharacterWrap]; 
CGRect frame = CGRectMake(0, 0, expectedLabelSize.width, expectedLabelSize.height);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.font = font;
label.textAlignment = UITextAlignmentLeft;
label.text = title;
self.titleView = label;

0
投票
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
lbl.text = @"Home";
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor = [UIColor clearColor];
lbl.textColor = [UIColor whiteColor];
lbl.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
lbl.shadowColor = [UIColor blackColor];
lbl.shadowOffset = CGSizeMake(0,1);
self.navigationItem.titleView = vw;
[self.navigationItem.titleView addSubview:lbl];

0
投票

对我有用的是在viewDidAppear方法中更新titleView框架。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    UIView *titleView = self.navigationItem.titleView;
    CGRect navBarFrame = self.navigationController.navigationBar.frame;
    [titleView setFrame:CGRectMake((CGRectGetWidth(navBarFrame) - TitleWidth) / 2, (CGRectGetHeight(navBarFrame) - TitleHeight) / 2, TitleWidth, TitleHeight)];
}
© www.soinside.com 2019 - 2024. All rights reserved.