尝试将子视图添加到导航栏

时间:2014-04-27 10:12:46

标签: objective-c ios7 uiview uinavigationbar

我尝试了什么:

UIView *view = [[UIView alloc] initWithFrame:frame];
[view setBackgroundColor:[UIColor redColor]];
[[UINavigationBar appearance] addSubview:view];     // does't work

[self.navController.view addSubview:view];          // does't work
[self.navController.view bringSubviewToFront:view]; // 

问题是我们如何在iOS7的导航栏中正确添加子视图?谢谢你的建议。

UPD0:对不起,伙计们。我得到了一部分。这是因为我在self.navController.navigationBarHidden = YES之后setupAppearance。好吧,似乎有一个有趣的导航脸。酒吧实施。 navigationBarHidden和每个视图中我们都有自定义导航。酒吧。我应该深入了解一下。无论如何,谢谢你的回复。

UPD1:只需继续搜索解决方案即可向nav添加自定义视图。酒吧喜欢背景图片。

2 个答案:

答案 0 :(得分:9)

您必须将其添加到导航栏,试试这个:

[self.navigationController.navigationBar addSubview:view];

答案 1 :(得分:1)

我是这样做的: 在我的主视图控制器中,我将主视图控制器初始化为UINavigationController作为委托(主视图需要实现UINavigationControllerDelegate):

self.nav = [[UINavigationController alloc] initWithRootViewController: self];

导航控制器将自动负责创建UINavigationBar。您还可以通过此导航控制器将子视图添加到UINavigationBar。以下是我添加子视图的方法:

UIView *buttonsView = [[UIView alloc] initWithFrame:buttonsFrame];
button = [self buttonWithName:@"crop" target:self selector:@selector(cropImage) left:left];
[buttonsView addSubview: button];
...
self.navigationItem.titleView = buttonsView;

这就是全部。

相关问题