Custom TabBar layout for UITabBarViewController

前端 未结 2 753
悲哀的现实
悲哀的现实 2020-12-12 04:47

Please refer to this Answer.

I am trying to do the same thing, however I want to do this in a Tab Bar App where the Now Playing bar is above the Tab Bar in all the s

相关标签:
2条回答
  • 2020-12-12 05:12

    Using UITabBarViewController subclass it is possible:

    Ex:

    class DashBoardViewController: UITabBarController {
    
        let nowPlayingBar:UIView = {
            let view = UIView(frame: .zero)
            view.backgroundColor = .blue
            return view
        }()
    
        override func viewDidLoad() {
            super.viewDidLoad()
            initView()
        }
    
        override func viewDidLayoutSubviews() {
            super.viewDidLayoutSubviews()
            nowPlayingBar.frame = tabBar.frame
        }
    
        override func viewDidAppear(_ animated: Bool) {
            var newSafeArea = UIEdgeInsets()
    
            // Adjust the safe area to accommodate
            //  the height of the bottom views.
            newSafeArea.bottom += nowPlayingBar.bounds.size.height
    
            // Adjust the safe area insets of the
            //  embedded child view controller.
            self.childViewControllers.forEach({$0.additionalSafeAreaInsets = newSafeArea})
        }
    
        private func initView() {
            nowPlayingBar.frame = tabBar.frame
            view.addSubview(nowPlayingBar)
        }
    }
    
    0 讨论(0)
  • 2020-12-12 05:17

    You'll add your view/container to your app window, you'd do something like

    guard let window = (UIApplication.shared.delegate as? AppDelegate)?.window 
         else { return } // check if there's a window 
    
        let containerHeight: CGFloat = 50  // height for the view where you wish to add the music player 
    
       let containerFrame = CGRect(x:0, y: window.frame.maxY - (tabBar.frame.height + containerHeight), width: window.frame.width, height: containerHeight)
       // most important part here is the y axis in some sense, you will add the height of the tabBar and the container, then subtract it from window.frame.maxY  
    
       let container = UIView(frame: containerFrame)
       // now you have the container do whatever you want with it 
       window.addSubView(container) // finally add the container to window as a subview 
    
    0 讨论(0)
提交回复
热议问题