请参阅此Answer。
我正在尝试做同样的事情,但是我想在Tab栏应用程序中执行此操作,其中“正在播放”栏位于应用程序的所有场景中的Tab栏上方。
更新:
我希望在屏幕底部(标签栏上方)和不同标签的内容视图(不在其上方)中显示视图。此外,我希望能够在某个点删除此视图,使主视图占据整个屏幕。
我可以使用提到的Answer通过以编程方式更改nowPlaying
视图的约束来执行此操作。
答案 0 :(得分:1)
使用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({$0.additionalSafeAreaInsets = newSafeArea})
}
private func initView() {
nowPlayingBar.frame = tabBar.frame
view.addSubview(nowPlayingBar)
}
}
答案 1 :(得分:0)
您将视图/容器添加到您的应用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