微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐