【发布时间】:2013-08-27 19:11:54
【问题描述】:
我正在尝试以编程方式将 UINavigationBar 添加到 UIView 并取得了如此成功,但无法弄清楚。我正在尝试将它添加到我的 UIView 子类中,但当我运行我的应用程序时它根本没有显示出来。
这是我的代码:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)];
[navBar setBackgroundColor:[UIColor blueColor]];
[self addSubview:navBar];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"];
[navItem setRightBarButtonItem:doneItem animated:YES];
[navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];
【问题讨论】:
-
你用什么方法得到这个代码?我刚刚尝试过,它对我有用。此外,设置 backgroundColor 不会有任何效果。你必须使用“tintColor”属性
-
它在
if (self)语句中的 init 方法期间被调用。我不明白为什么这会阻止它工作,因为我在这里添加了其他子视图。 -
嗯,这确实很奇怪,因为我刚刚再次尝试重现您的场景并且它对我有用。我相信问题可能是 'headerHeight' 设置为 0。
-
忘了包括那个。设置为 44.0。
-
"barTintColor"属性。
标签: ios objective-c uiview uinavigationbar