gpt4 book ai didi

ios - 使用 UITabBarController 的持久 View

转载 作者:行者123 更新时间:2023-12-05 07:33:44 25 4
gpt4 key购买 nike

我正在尝试构建一个布局类似于 Apple Music 的应用程序 - 具有持久 View 的标签栏导航,可从应用程序的任何位置访问。 View 可以展开以占据整个屏幕或最小化为 80 的静态高度。UI 构建在带有普通 UITabBarController 的 Storyboard 中。这是初稿:1]

这是我构建它的方式:

class TabbarViewController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()
embedLiveFeedbackController()
}

private func embedLiveFeedbackController() {
guard let feedbackController = UIStoryboard(name: "LiveFeedback", bundle: nil).instantiateInitialViewController() as? LiveFeedbackViewController else { return }

feedbackController.stateDelegate = self
addChildViewController(feedbackController)
view.addSubview(feedbackController.view)

feedbackController.view.translatesAutoresizingMaskIntoConstraints = false
feedbackController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
feedbackController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
feedbackController.view.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true

liveFeedbackTopConstraint = feedbackController.view.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor)
liveFeedbackHeightConstraint = feedbackController.view.heightAnchor.constraint(equalToConstant: Constants.minimizedHeight)

liveFeedbackHeightConstraint?.isActive = true
liveFeedbackTopConstraint?.isActive = false

}
}

我遇到的问题是 View Controller 的内容落后于持久 View 并且不完全可见。我尝试过的一件事是将 View Controller 限制在持久 View 的顶部:

private func constraintViewControllers() {
guard let vcs = viewControllers else { return }
guard let topAnchor = liveFeedbackTopAnchor else { return } // a reference to the top anchor of the persistent view

for viewController in vcs {
viewController.view.translatesAutoresizingMaskIntoConstraints = false
viewController.view.bottomAnchor.constraint(equalTo: topAnchor).isActive = true
}
}

当然,我得到以下错误:

Unable to activate constraint with anchors <NSLayoutYAxisAnchor:0x600000864640 "UILayoutContainerView:0x7fc813f0ea60.bottom"> and <NSLayoutYAxisAnchor:0x60400047b980 "UIView:0x7fc813f08af0.bottom"> because they have no common ancestor.  Does the constraint or its anchors reference items in different view hierarchies?  That's illegal.'

有什么关于如何实现的建议吗?

最佳答案

这是我的建议,我测试过,效果很好

创建一个主 viewController 并在其中放置 2 个 View (如图所示):

enter image description here

-startViewController 
--containerView (your app root viewController/tabBarController goes here)
--persistentView

在 startViewController

class ViewController: UIViewController {

var tabController : TabController!

@IBOutlet weak var containerView: UIView!
@IBOutlet weak var persistentView: UIView!
@IBOutlet weak var persistentBottomConstraint: NSLayoutConstraint!

override func viewDidLoad() {
super.viewDidLoad()

tabController = TabController.initFromStoryBord()

self.addChild(tabController)
tabController.view.frame = containerView.bounds
containerView.addSubview(tabController.view)
tabController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]


persistantBottomConstraint.constant = tabController.tabBar.frame.height
}
}

然后是 TabBarController 类:

确保为 tabController 设置 Stroyboard ID

class TabController : UITabBarController {


override func viewDidLoad() {
super.viewDidLoad()
}

static func initFromStoryBord() -> TabController {
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "TabController") as! TabController
return vc
}
}

关于ios - 使用 UITabBarController 的持久 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50436145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com