【问题标题】:Custom TabBar layout for UITabBarViewControllerUITabBarViewController 的自定义 TabBar 布局
【发布时间】:2018-06-25 17:48:41
【问题描述】:

请参考这个Answer

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

更新:

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

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

【问题讨论】:

  • 子类 UITabBarViewcontroller 并在 ViewDidLoad(:) 上添加 CustomView

标签: ios swift uitabbarcontroller uitabbar


【解决方案1】:

你将把你的视图/容器添加到你的应用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 

【讨论】:

    【解决方案2】:

    使用 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)
        }
    }
    

    【讨论】:

    • 这是替换标签栏。有没有办法让它在标签栏上方?所以在每个场景中我都会有两者。
    • 可以设置帧的偏移量
    • 试试这个代码:nowPlayingBar.frame = tabBar.frame.offsetBy(dx: 0, dy: -tabBar.frame.height)
    • 添加`nowPlayingBar.frame = tabBar.frame.offsetBy(dx: 0, dy: -tabBar.frame.height)`使它出现在标签栏上方,但现在它显示在内容上方看法 。有没有办法在正在播放的视图和内容视图之间设置约束。请检查更新后的问题。
    • 我想我会尝试调整内容视图的高度,使其高于正在播放的栏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 2015-04-05
    相关资源
    最近更新 更多