【问题标题】:Trying to add subview to navigation bar尝试将子视图添加到导航栏
【发布时间】:2014-04-27 10:12:46
【问题描述】:

我尝试了什么:

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:对不起,伙计们。我得到了一部分。这是因为我在setupAppearance 之后有self.navController.navigationBarHidden = YES。好吧,似乎有一张有趣的导航。酒吧实施。 navigationBarHidden 在每个视图中我们都有自定义导航。酒吧。我应该深入了解细节。无论如何感谢您的回复。

UPD1:只需继续搜索解决方案即可将自定义视图添加到导航。条形背景图片。

【问题讨论】:

    标签: objective-c ios7 uiview uinavigationbar


    【解决方案1】:

    你必须将它添加到导航栏,试试这个:

    [self.navigationController.navigationBar addSubview:view];
    

    【讨论】:

    • 没有这个字段navController
    • navController 可能是未显示的提问者 self 类的属性。提问者没有发布课程,所以我们不能肯定地说,只是猜测。
    【解决方案2】:

    我是这样做的: 在我的主视图控制器中,我使用主视图控制器作为委托初始化 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;
    

    就是这样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-19
      • 2010-12-05
      相关资源
      最近更新 更多