类似于播放歌曲时 Spotify 或 Apple Music 应用程序所做的,它将自定义 View 放置在 UITabBar 之上:
我尝试过的解决方案:
ViewController 中的 UITabBarController 具有最大尺寸的容器 View ,自定义 View 位于底部布局指南上方的容器 View49pt 顶部: 问题:嵌入在 UITabBarController 中的 ViewController 中的任何内容都不会显示,因为它们隐藏在自定义布局后面。我尝试在 UITabBarController 中覆盖
size forChildContentContainer
,尝试更新底部布局指南,Nothing。 我需要调整 UITabBarController 容器 View 的框架。再次尝试 #1,但尝试通过 increasing the size of UITabBar 解决隐藏在它后面的内容的问题, 然后使用 ImageInset on every TabBarItem将其关闭,并在 UITabBar 顶部添加我的自定义 View 。效果不是很好。有时我想隐藏我的自定义 View 。
UITabBarController 作为根,每个 child 都是一个 ViewController,带有容器 View + 我的自定义 View : 但是现在我有多个自定义 View 实例在四处 float 。如果我想更改它上面的标签,必须将其更改为所有 View 。或者隐藏等
覆盖 UITabBarController 的 UITabBar 属性并返回我的自定义 UITabBar(用 xib 对其进行膨胀),它有一个 UITabBar + 我的自定义 View 。 问题:可能是所有尝试中最令人沮丧的尝试。如果您使用
class MyCustomTabBar : UITabBar {}
的实例覆盖该属性,则不会显示任何选项卡!是的,我将myCustomTabBar
的委托(delegate)设置为self
。
倾向于 #3,但正在寻找更好的解决方案。
最佳答案
如果您继承 UITabBarController 并以编程方式添加您的 View ,这实际上非常容易。使用此技术自动支持标签栏的旋转和大小更改,无论您使用的是哪个版本。
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
//...do some of your custom setup work
// add a container view above the tabBar
let containerView = UIView()
containerView.backgroundColor = .red
view.addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
containerView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
// anchor your view right above the tabBar
containerView.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true
containerView.heightAnchor.constraint(equalToConstant: 50).isActive = true
}
}
关于ios - 在 UITabBar 上查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42384470/