【问题标题】:How to add UIView over navigation bar?如何在导航栏上添加 UIView?
【发布时间】:2013-01-09 09:44:48
【问题描述】:

我需要像这里一样覆盖UINavigationBarUIView

除了使用带有按钮的自定义 UIView 作为导航栏之外,还有其他方法吗?

【问题讨论】:

  • 如果你想做这种事情比使用 UIView 创建的自定义导航更好。比你完全控制它,你可以给你想要的任何外观......

标签: ios objective-c uiview uinavigationbar


【解决方案1】:

您可以在应用程序的基础视图中添加子视图

[[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];

为了确保它仅在您的视图控制器可见时显示,您可以在 viewDidAppearviewDidDisappear 委托方法中添加和删除它。这是一个显示与它们重叠的蓝色框的示例。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.    
    vTestView = [[UIView alloc] initWithFrame:CGRectMake(10.0f,
                                                         10.0f,
                                                        100.0f,
                                                        100.0f)];
    vTestView.backgroundColor = [UIColor blueColor];
}


-(void)viewDidAppear:(BOOL)animated
{
    [[[UIApplication sharedApplication] keyWindow] addSubview:vMyCustomUIView];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [vMyCustomUIView removeFromSuperview];
}

【讨论】:

  • 在 swift 2... 让覆盖: UIView = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.size.width, UIScreen.mainScreen().bounds.size .height)) overlay.backgroundColor = UIColorFromHex(0x000000, alpha: 0.7) UIApplication.sharedApplication().keyWindow!.addSubview(overlay)
【解决方案2】:

使用方法:

- (void)bringSubviewToFront:(UIView *)view

所以你的情况是:

[navigationBar addSubview:myRibbon];
[navigationBar bringSubviewToFront:myRibbon];

另外不要忘记,通过这种方式,您的功能区不会完全显示,除非您这样做:

navigationBar.clipsToBounds = NO;

【讨论】:

    【解决方案3】:

    我隐藏 NavigationBar 并添加了 UIView。它看起来与导航栏相同。 然后添加更多带有红色书签的 UIView。除此之外,它还可以在触摸时单独为书签设置动画。

    如果我以这种方式添加书签: [navigationBar addSubview:myRibbon]; 它隐藏在一半大小上

    【讨论】:

      【解决方案4】:

      这是我的实现方式,希望对您有所帮助..

        override func viewWillAppear(_ animated: Bool) {
          let testView = UIView(frame: .zero)
          testView.backgroundColor = .black
          testView.layer.cornerRadius = 10
          testView.translatesAutoresizingMaskIntoConstraints = false
          self.navigationController?.navigationBar.addSubview(testView)
          NSLayoutConstraint.activate([
            testView.widthAnchor
              .constraint(equalToConstant: 50),
            testView.heightAnchor
              .constraint(equalToConstant: 70),
            testView.topAnchor
              .constraint(equalTo: (self.navigationController?.navigationBar.topAnchor)!),
            testView.trailingAnchor
              .constraint(equalTo: (self.navigationController?.navigationBar.trailingAnchor)!, constant: -20)
            ])
          self.customView = testView
        }
        
        // i dont need to display the overlay in every page
        // so i remove it everytime i navigate to a new page
        override func viewWillDisappear(_ animated: Bool) {
          super.viewWillDisappear(animated)
          self.customView.removeFromSuperview()
        }
      

      【讨论】:

        【解决方案5】:

        这是一个 Swift 3 答案

        UIApplication.shared.keyWindow?.addSubview(menuView)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-03
          • 1970-01-01
          • 1970-01-01
          • 2016-09-23
          • 1970-01-01
          • 2015-04-26
          • 1970-01-01
          相关资源
          最近更新 更多