【发布时间】:2018-10-30 08:39:07
【问题描述】:
我正在尝试构建一个布局类似于 Apple Music 的应用 - 一个带有持久视图的标签栏导航,可从应用中的任何位置访问。视图可以扩展以占据整个屏幕或最小化,静态高度为 80。UI 构建在带有普通UITabBarController 的故事板中。这是初稿:
这就是我构建它的方式:
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
}
}
我遇到的问题是视图控制器的内容位于持久视图之后并且不完全可见。我尝试过的一件事是将视图控制器限制在持久视图的顶部:
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.'
有什么建议如何实施吗?
【问题讨论】:
-
每次我在 SO 上问这样的问题时,我都会得到类似的回答(几乎没有)。我开始相信滚动视图的内容插入和自动调整它们并没有被广泛理解。而那些理解它的人通常不愿意回答。我很想知道您是否得到任何有见地的反馈。
-
如果我现在有更多时间来探索这个问题,我会更多地关注
UIViewController.additionalSafeAreaInsets和UIScrollView.adjustedContentInset。您可能可以在viewWillLayoutSubviews或viewDidLayoutSubviews期间调整您的UITabController的additionalSafeAreaInsets(或其子视图控制器)(我不完全确定这会起作用,但可能会让您正确追踪)。对于 iOS11 之前的版本,我可能会研究某种基于UILayoutGuide的解决方案。 -
对不起,我帮不了你了。但我希望这足以让你走上正轨。如果您确实找到了一个好的解决方案,我很乐意看到它,我相信其他人也会从答案中受益。
标签: ios swift uitabbarcontroller