UITabBarViewController 的自定义 TabBar 布局

Custom TabBar layout for UITabBarViewController

请参考这个Answer

我正在尝试做同样的事情,但是我想在标签栏应用程序中执行此操作,其中正在播放栏在应用程序的所有场景中都位于标签栏上方。

更新:

我想在屏幕底部(选项卡栏上方)和不同选项卡的内容视图下方(而不是在它们上方)有一个视图。此外,我希望能够在某个点删除此视图,使主视图占据整个屏幕。

我可以使用提到的 Answer 通过以编程方式更改 nowPlaying 视图的约束来做到这一点。

您将 view/container 添加到您的应用程序 window,您会执行类似

的操作
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 

使用 UITabBarViewController 子类是可能的:

例如:

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({[=10=].additionalSafeAreaInsets = newSafeArea})
    }

    private func initView() {
        nowPlayingBar.frame = tabBar.frame
        view.addSubview(nowPlayingBar)
    }
}