15

UINavigationBar我需要像UIView这里一样覆盖

http://screencast.com/t/ZKXNFcAzVu

除了使用带有按钮的自定义 UIView 作为导航栏之外,有没有办法做到这一点?

4

5 回答 5

24

您可以将子视图添加到应用程序的基本视图

[[[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];
}
于 2013-04-12T16:21:06.970 回答
14

使用方法:

- (void)bringSubviewToFront:(UIView *)view

所以在你的情况下,这将是:

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

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

navigationBar.clipsToBounds = NO;
于 2013-04-12T16:29:05.447 回答
1

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

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

于 2013-04-15T06:51:00.793 回答
1

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

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

导航栏覆盖

于 2020-01-10T06:42:07.533 回答
0

这是一个 Swift 3 的答案

UIApplication.shared.keyWindow?.addSubview(menuView)
于 2017-08-22T20:07:11.533 回答