gpt4 book ai didi

ios - 导航栏和 ViewControllers View 之间的 NSLayoutConstraint

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:51:15 25 4
gpt4 key购买 nike

我们可以在 self.navigationcontroller.navigationbarself.view 中的 View 之间添加一个 NSLayoutConstraint 吗?这里的 self 是一个 UIViewController 实例,_textFieldself.view

的 subview

我需要的是无论 navigationBar 是否半透明,UI 都应该看起来相似。

我尝试了以下方法。但它不起作用。

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:_textField
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:self.navigationController.navigationBar attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:20];
[self.navigationcontroller.view addConstraint:cn];

最佳答案

是的,您可以在导航栏和 View 之间添加约束。添加到导航 Controller 的 Root View Controller 包含 topLayoutGuide。所以像这样调整你的代码:

NSLayoutConstraint* cn = [NSLayoutConstraint constraintWithItem:_textField
attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual
toItem:self.rootViewController.topLayoutGuide attribute:NSLayoutAttributeBottom
multiplier:1.0 constant:20];
[self.rootViewController.view addConstraint:cn];

请注意,我根本没有引用导航 Controller ,而是导航 Controller 的 rootViewController。

您也可以使用 bottomLayoutGuide 以相同的方式移动到 TabBar 上方。 (但是,如果您需要这样做,您会在 iOS 框架中遇到一个错误,这里有一个解决方法补丁:UIViews ending up beneath tab bar)

关于ios - 导航栏和 ViewControllers View 之间的 NSLayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17075179/

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