我正在尝试以编程方式在我的 UIViewController 实现中添加两个标签 - 一个在 UINavigationBar 的左侧,一个在右侧。但是当我运行我的程序时它没有出现。我有一个嵌入在 UINavigationController 中的 UIViewController。我正在使用以下代码:
- (void) setTheNavigationbarView{
CGFloat originX = self.navigationItem.titleView.bounds.origin.x;
CGFloat originY = self.navigationItem.titleView.bounds.origin.y;
CGFloat sizeW = self.navigationItem.titleView.bounds.size.width;
CGFloat sizeH = self.navigationItem.titleView.bounds.size.height;
UIView * navigationbarView = [[UIView alloc] initWithFrame:CGRectMake(originX + 1, originY + 1, sizeW - 2, sizeH - 2)];
navigationbarView.backgroundColor = [UIColor clearColor];
UILabel *labelLeft = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, (sizeW - 2)/2, (sizeH - 2))] autorelease];
labelLeft.backgroundColor = [UIColor clearColor];
labelLeft.font = [UIFont boldSystemFontOfSize:16];
labelLeft.adjustsFontSizeToFitWidth = NO;
labelLeft.textAlignment = UITextAlignmentLeft;
labelLeft.textColor = [UIColor blackColor];
labelLeft.text = @"first line";
labelLeft.highlightedTextColor = [UIColor blackColor];
[navigationbarView addSubview:labelLeft];
UILabel *labelRight = [[[UILabel alloc] initWithFrame:CGRectMake(((sizeW - 2)/2 + 1), 0, (sizeW - 2)/2, (sizeH - 2))] autorelease];
labelRight.backgroundColor = [UIColor clearColor];
labelRight.font = [UIFont boldSystemFontOfSize:16];
labelRight.adjustsFontSizeToFitWidth = NO;
labelRight.textAlignment = UITextAlignmentRight;
labelRight.textColor = [UIColor blackColor];
labelRight.text = @"second line";
labelRight.highlightedTextColor = [UIColor blackColor];
[navigationbarView addSubview:labelRight];
// set the view as navigationba title view
self.navigationItem.titleView = navigationbarView;
}
我将不胜感激。谢谢。