View on Top of UITabBar

后端 未结 7 1793
慢半拍i
慢半拍i 2021-02-02 16:03

Similar to what the Spotify or Apple Music app does when a song is playing, it places a custom view on top of the UITabBar:

Solutions I\'ve tried:

  1. UITa

7条回答
  •  终归单人心
    2021-02-02 16:50

    This is actually very easy if you subclass UITabBarController and add your view programmatically. Using this technique automatically supports rotation and size changes of the tab bar, regardless of which version you are on.

    class CustomTabBarController: UITabBarController {
      override func viewDidLoad() {
        super.viewDidLoad()
    
        //...do some of your custom setup work
        // add a container view above the tabBar
        let containerView = UIView()
        containerView.backgroundColor = .red
        view.addSubview(containerView)
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        containerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
    
        // anchor your view right above the tabBar
        containerView.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true
    
        containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
      }
    }
    

提交回复
热议问题