gpt4 book ai didi

ios - 如何在代码中创建与 @"H:|-10-[view]"完全相同的 NSLayoutConstraint 对象?

转载 作者:行者123 更新时间:2023-11-28 19:56:18 24 4
gpt4 key购买 nike

我一直在尝试弄清楚如何使用 NSLayoutConstraint 的 constraintWithItem 方法创建一个 NSLayoutConstraint 对象,该方法等同于下面的代码。

NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:"@H:|-10-[view]"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(view)];

NSLayoutConstraint *rightMarginConstraint = [constraints firstObject];

我试过如下代码:

NSLayoutConstraint* rightMarginConstraints = [NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:superview
attribute:NSLayoutAttributeLeft
multiplier:1.0f
constant:10.0f];

但它做的事情并不完全相同,打印出来时在控制台上也不同。我想要一个在控制台上打印为 "H:|-10-[view]" 的约束。

有谁知道如何创建一个同时适用于 iOS7 和 8 的软件? (我发现 iOS 8 有新的属性选项,但我仍然支持 iOS 7)

提前致谢!

/////////编辑////////

实际上,第二个代码块对简单 View 和父 View 产生了完全相同的约束。但是,我在 TableViewCells 上遇到问题,结果如下:

这就是我想要的,用类似于第一 block 代码的代码生成

<NSLayoutConstraint:0x7fc050f481c0 H:|-(10)-[UIView:0x7fc050f48070]   (Names:     '|':UITableViewCellContentView:0x7fc050f39310 )>

第二个代码块产生类似于下面输出的约束

<NSLayoutConstraint:0x7b6513d0 view:0x7b64cdf0.leading == UITableViewCellContentView:0x7b64cd10.left>

///编辑2///即使我使用了匹配的属性,我的输出也看起来像下面的代码。

<NSLayoutConstraint:0x7d695c70 CustomBorderView:0x7d4cb0f0.right == UITableViewCellContentView:0x7d4cb010.right + 10>

///编辑3///

我认为项目的顺序很重要。我交换了 constraintWithItem: 和 toItem: 的参数,现在输出我想要的。谢谢!

最佳答案

NSLayoutAttributeLeftNSLayoutAttributeLeading 不同。试试这个:

NSLayoutConstraint* rightMarginConstraints = [NSLayoutConstraint
constraintWithItem:view attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:superview attribute:NSLayoutAttributeLeading
multiplier:1.0f constant:10.0f];

或者这个:

NSLayoutConstraint* rightMarginConstraints = [NSLayoutConstraint
constraintWithItem:view attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:superview attribute:NSLayoutAttributeLeft
multiplier:1.0f constant:10.0f];

这两个都给了我你想要的输出。混合 ...Left...Leading 给我你不需要的输出。

关于ios - 如何在代码中创建与 @"H:|-10-[view]"完全相同的 NSLayoutConstraint 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26225020/

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