与Spotify或Apple应用程序在播放歌曲时所做的类似,它在UITabBar之上放置了一个自定义视图:

我尝试过的解决方案:

问题:嵌入到UITabBarController底部的ViewControllers中的任何内容都不会显示,因为它们隐藏在自定义布局后面。我尝试过在size forChildContentContainer中重写UITabBarController,尝试更新底层布局指南,什么都没有。我需要调整UITabBarController.的容器视图框架的大小。

但是现在我有了多个自定义视图的实例。如果我想更改标签,必须将其更改为所有视图。或隐藏等
class MyCustomTabBar : UITabBar {}实例覆盖该属性,则不会出现选项卡!是的,我将myCustomTabBar的代表设置为self。倾向于#3,但寻找一个更好的解决方案。
发布于 2018-01-09 23:55:47
如果您将UITabBarController子类并以编程方式添加视图,这实际上非常容易。使用此技术可以自动支持选项卡条的旋转和大小更改,而不管您使用的是哪个版本。
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
}
}发布于 2017-02-23 19:30:48
这样啊,原来是这么回事!

本质上,我增加了原始UITabBar的大小,以容纳自定义视图(并缩小上面视图控制器的框架),然后在其之上添加一个重复的UITabBar +自定义视图。
这是我必须做的事情的要点。我上传了一个实用的示例,可以是在这次回购中发现。
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
}
}发布于 2020-06-10 15:06:45
自从iOS 11之后,这就变得简单了一些。添加视图时,可以执行以下操作:
viewControllers?.forEach {
$0.additionalSafeAreaInsets = UIEdgeInsets(
top: 0,
left: 0,
bottom: yourView.height,
right: 0
)
}https://stackoverflow.com/questions/42384470
复制相似问题