How to add UIView over navigation bar?

前端 未结 5 438
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 15:38

I need overlay UINavigationBar with UIView like here

\"http://screencast.com/t/ZKXNFcAzVu

相关标签:
5条回答
  • 2021-02-02 15:42

    You can add a subview to the base view of the Application

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

    To make sure it is only shown when your view controller is visible you could add and remove it in the viewDidAppear and viewDidDisappear delegate methods. Here is an example that would show a blue box overlapping them.

    - (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];
    }
    
    0 讨论(0)
  • 2021-02-02 15:43

    Here's how implemented mine, hope this helps..

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

    0 讨论(0)
  • 2021-02-02 15:45

    I hide NavigationBar and added UIView instead. It looks the same as Navbar. then add more one UIView with red bookmark. Beside it give possibility to animate bookmark on touch separately.

    If I added bookmark this way: [navigationBar addSubview:myRibbon]; it was hidden on half of size

    0 讨论(0)
  • 2021-02-02 15:46

    Here's a Swift 3 answer

    UIApplication.shared.keyWindow?.addSubview(menuView)
    
    0 讨论(0)
  • 2021-02-02 15:52

    Use the method :

    - (void)bringSubviewToFront:(UIView *)view
    

    So in your case that will be :

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

    Also don't forget that with this way, your ribbon won't be showed completely except if you do this :

    navigationBar.clipsToBounds = NO;
    
    0 讨论(0)
提交回复
热议问题