ios – 在UITabBar上查看

与播放歌曲时Spotify或Apple Music应用程序的功能类似,它会在UITabBar上放置一个自定义视图:

我试过的解决方案:

>具有最大容器视图的ViewController中的UITabBarController,以及底部布局指南上方Container View49pt顶部的自定义视图:

问题:嵌入在底部的UITabBarController中的ViewControllers中的任何内容都不会显示,因为它们隐藏在自定义布局后面.我已经尝试在UITabBarController中覆盖大小forChildContentContainer,尝试更新底部布局指南,nothing.我需要调整UITabBarController的容器视图的框架.
>再次尝试#1,但尝试在increasing the size of UITabBar之前解决隐藏在其后面的内容问题,然后使用ImageInset on every TabBarItem将其关闭,并在UITabBar上添加我的自定义视图.工作得不好.有时候我想隐藏自定义视图.
> UITabBarController作为root用户,每个子节点都是ViewController,带有Container View我的自定义视图:

但现在我有多个自定义视图实例浮动.如果我想更改其上的标签,则必须将其更改为所有视图.或隐藏等
>覆盖UITabBarController的UITabBar属性并返回我的自定义UITabBar(用xib充气),它具有UITabBar我的自定义视图.问题:可能是所有人最令人沮丧的尝试.如果使用类MyCustomTabBar:UITabBar {}的实例覆盖该属性,则不会显示任何选项卡!是的,我将myCustomTabBar的委托设置为self.

倾向于#3,但寻找更好的解决方案.

解决方法

我知道了!

本质上,我增加了原始UITabBar的大小以容纳自定义视图(并缩小上面的viewcontrollers的框架),然后在其上添加一个重复的UITabBar自定义视图.

这是我必须要做的事情.我上传一个功能实例,可以是found in this repo

class TabBarViewController: UITabBarController {

    var currentlyPlaying: CurrentlyPlayingView!
    static let maxHeight = 100
    static let minHeight = 49
    static var tabbarHeight = maxHeight

    override func viewDidLoad() {
        super.viewDidLoad()

        currentlyPlaying = CurrentlyPlayingView(copyFrom: tabBar)
        currentlyPlaying.tabBar.delegate = self

        view.addSubview(currentlyPlaying)
        tabBar.isHidden = true
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        currentlyPlaying.tabBar.items = tabBar.items
        currentlyPlaying.tabBar.selectedItem = tabBar.selectedItem
    }
    func hideCurrentlyPlaying() {
        TabBarViewController.tabbarHeight = TabBarViewController.minHeight
        UIView.animate(withDuration: 0.5,animations: {
            self.currentlyPlaying.hideCustomView()
            self.updateSelectedViewControllerLayout()
        })
    }
    func updateSelectedViewControllerLayout() {
        tabBar.sizetoFit()
        tabBar.sizetoFit()
        currentlyPlaying.sizetoFit()
        view.setNeedsLayout()
        view.layoutIfNeeded()
        viewControllers?[self.selectedindex].view.setNeedsLayout()
        viewControllers?[self.selectedindex].view.layoutIfNeeded()
    }
}

extension UITabBar {

    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        var sizeThatFits = super.sizeThatFits(size)
        sizeThatFits.height = CGFloat(TabBarViewController.tabbarHeight)
        return sizeThatFits
    }
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...