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

标签 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]; 

最佳答案

这里的问题是您直接在 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/

相关文章:

ios - 从 UIPickerview 选择数据时如何以字符串格式显示 UIlabel

objective-c - 隐藏 UINavigationBar 时的自定义动画

ios - 从 searchController 解散后如何隐藏导航栏

swift - 如何以编程方式创建 Unwind segue

ios - 通过弹出 navigationController 或使用选项卡栏来识别是否到达 viewController 的更好方法

ios - 为 View 中的所有标签设置自定义字体

iOS 7 View 布局和半透明效果

快速 UINavigationController 委托(delegate)

ios - 从导航栏 View Controller 导航到标签栏 View Controller

ios - 具有多个链接的 TextView 或 UI 标签