【问题标题】:How to add a custom UIView to Navigation Bar in iOS7+ like this如何像这样在 iOS7+ 中向导航栏添加自定义 UIView
【发布时间】:2015-04-26 11:02:48
【问题描述】:

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

【问题讨论】:

    标签: ios ios7 uiview ios8 uinavigationbar


    【解决方案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];
    

    【讨论】:

      【解决方案2】:

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

      - (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;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 2011-08-21
        相关资源
        最近更新 更多