如何在iOS7 +中向导航栏添加自定义UIView,如下所示

时间:2015-02-24 17:26:03

标签: ios ios7 uiview ios8 uinavigationbar

有没有办法在UIView中插入任何NavigationBar,如下图所示?

enter image description here

2 个答案:

答案 0 :(得分:1)

这不起作用吗?

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];

[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: @"Controls"];
[navBar pushNavigationItem:navItem animated:NO];

答案 1 :(得分:1)

在下面的代码中,我在导航栏中嵌入了一个视图,我的嵌入视图绘制了背景和新按钮。该代码还涉及在旋转设备时中继视图。

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    if (view.superview == nil) {
        [self.navigationBar addSubview:view];
        [self.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    }

    CGRect frame = self.navigationBar.frame;
    frame.size.height = MY_NAVBAR_HEIGHT;
    self.navigationBar.frame = frame;
    view.frame = self.navigationBar.bounds;
}