UINavigationBar:添加具有移动效果的UILabel

时间:2012-01-17 15:29:57

标签: uinavigationcontroller uilabel uinavigationbar

我创建了UINavigationController应用程序。我将UILabel添加到UINavigationBar,但是有一个问题。当UINavigationBar向左或向右移动时,我的标签不会移动。我该如何解决这个问题?

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(70, 0, 200, 44)] autorelease];
label.text = @"There are too car on the ...";
label.backgroundColor = [UIColor clearColor];
[self.navigationController.navigationBar addSubview:label]; 

1 个答案:

答案 0 :(得分:3)

这里的问题是您直接在UINavigationBar上设置标签。如果您不需要自定义标签并且默认行为足够,则应设置 UIViewController标题属性。但是,如果您需要自定义标签,则应设置 UIViewController的navigationItem titleView 属性。看看下面的例子。我假设 self 是一个活动的视图控制器:

// for a simple title, just set the view controllers title property
[self setTitle:@"There are too car on the ...";

// for a custom label, you need to set navigationItem.title property
UILabel *label = [[[UILabel alloc] init] autorelease];
label.text = @"There are too car on the ...";
label.textColor = [UIColor yellowColor];
label.backgroundColor = [UIColor redColor];
[self.navigationItem setTitleView:label];