我创建了 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];
最佳答案
这里的问题是您直接在 UINavigationBar 上设置标签。如果您不需要自定义标签并且默认行为足以满足您的需求,则应该设置 UIViewController title 属性。但是,如果您需要自定义标签,则应该设置 UIViewController 的 navigationItem titleView 属性。看看下面的例子。我假设 self 是一个事件 View Controller :
// 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];
关于uinavigationcontroller - UINavigationBar:添加具有移动效果的UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897271/