约束到 UINavigationController navigationBar 锚点
Constrain to UINavigationController navigationBar anchors
我正在尝试将视图限制为 100% 类似于 UINavigationController
的 navigationBar
。以下无效…
view.addSubview(someView)
someView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
someView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor),
someView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor),
someView.topAnchor.constraint(equalTo: navigationBar.topAnchor),
someView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)
])
但是将前三个约束中的任何 替换为 view
会使 someView
可见。这例如有效:
NSLayoutConstraint.activate([
someView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor),
someView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor),
someView.topAnchor.constraint(equalTo: view.topAnchor),
someView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)
])
在另一侧的 UITabBarController
内限制为 tabBar
即使将所有内容限制为 tabBar
:
也是有效的
NSLayoutConstraint.activate([
someView.leadingAnchor.constraint(equalTo: tabBar.leadingAnchor),
someView.trailingAnchor.constraint(equalTo: tabBar.trailingAnchor),
someView.topAnchor.constraint(equalTo: tabBar.topAnchor),
someView.bottomAnchor.constraint(equalTo: tabBar.bottomAnchor)
])
我对解决方法没问题,但我想知道为什么它在单独限制为 navigationBar
时不起作用。有没有更好的方法让someView
和navigationBar
有相同的大小和位置?
如果您将您的子视图添加为 view
的子视图并且您将约束设置为 navigationBar
,它们没有共同的祖先,因为您会收到错误消息:
'Unable to activate constraint with anchors < NSLayoutXAxisAnchor:xxx "UIView:xxx.leading" > and < NSLayoutXAxisAnchor:xxx "UINavigationBar:xxx.leading" > because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
... 看起来如果您设置至少一个约束等于 view
,现在您的 someView
知道它与其父视图 view
以及navigationBar
.
解决问题的一种方法是将视图添加到 NavigationBar
navigationBar.addSubview(someView)
注意:我已经测试了您的代码,它不适用于设置等于 TabBar 锚点的约束
我正在尝试将视图限制为 100% 类似于 UINavigationController
的 navigationBar
。以下无效…
view.addSubview(someView)
someView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
someView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor),
someView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor),
someView.topAnchor.constraint(equalTo: navigationBar.topAnchor),
someView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)
])
但是将前三个约束中的任何 替换为 view
会使 someView
可见。这例如有效:
NSLayoutConstraint.activate([
someView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor),
someView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor),
someView.topAnchor.constraint(equalTo: view.topAnchor),
someView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor)
])
在另一侧的 UITabBarController
内限制为 tabBar
即使将所有内容限制为 tabBar
:
NSLayoutConstraint.activate([
someView.leadingAnchor.constraint(equalTo: tabBar.leadingAnchor),
someView.trailingAnchor.constraint(equalTo: tabBar.trailingAnchor),
someView.topAnchor.constraint(equalTo: tabBar.topAnchor),
someView.bottomAnchor.constraint(equalTo: tabBar.bottomAnchor)
])
我对解决方法没问题,但我想知道为什么它在单独限制为 navigationBar
时不起作用。有没有更好的方法让someView
和navigationBar
有相同的大小和位置?
如果您将您的子视图添加为 view
的子视图并且您将约束设置为 navigationBar
,它们没有共同的祖先,因为您会收到错误消息:
'Unable to activate constraint with anchors < NSLayoutXAxisAnchor:xxx "UIView:xxx.leading" > and < NSLayoutXAxisAnchor:xxx "UINavigationBar:xxx.leading" > because they have no common ancestor. Does the constraint or its anchors reference items in different view hierarchies? That's illegal.'
... 看起来如果您设置至少一个约束等于 view
,现在您的 someView
知道它与其父视图 view
以及navigationBar
.
解决问题的一种方法是将视图添加到 NavigationBar
navigationBar.addSubview(someView)
注意:我已经测试了您的代码,它不适用于设置等于 TabBar 锚点的约束