我正在尝试构建一个布局类似于Apple Music的应用程序 - 一个带有持续视图的选项卡式导航,可从应用程序的任何位置访问。该视图可以扩展以占据整个屏幕,也可以缩小为静态高度为80。UI是在Storyboard中使用普通的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.'
有什么建议如何实施这个功能?
UIViewController.additionalSafeAreaInsets
和UIScrollView.adjustedContentInset
。你可能可以在viewWillLayoutSubviews
或viewDidLayoutSubviews
期间调整你的UITabController
的additionalSafeAreaInsets
(或其子视图控制器的additionalSafeAreaInsets
)。(我不完全确定这样做是否有效,但可能会让你走上正确的轨道)。对于iOS11之前的版本,我可能会考虑一些基于UILayoutGuide
的解决方案。 - ABeard89