gpt4 book ai didi

ios - AutoLayout 在 ios 8 中有效但在 ios 7 中无效?

转载 作者:可可西里 更新时间:2023-11-01 04:23:24 25 4
gpt4 key购买 nike

我在 UICollectionViewCell 中使用自动布局。 super 简单的布局:只是一个 UILabel。我希望 UILabel 占据整个宽度减去 20 像素的插图,并在垂直和水平方向上居中于单元格中。我已经设置了这样做的约束。如果我在任何 ios 8 设备或模拟器上运行它,它都能完美运行。但是,当我在某些 ios 7 设备上运行它时,约束无效。我尝试查看苹果文档,但他们的更改似乎都与自动布局无关。

这是 XML 源代码,但我怀疑它是否有意义:

<constraints>
<constraint firstItem="OIc-cg-9hO" firstAttribute="leading" secondItem="Ga6-nx-lOn" secondAttribute="leading" constant="20" id="A7U-sd-fcL"/>
<constraint firstAttribute="centerY" secondItem="OIc-cg-9hO" secondAttribute="centerY" id="G9e-9W-aDS"/>
<constraint firstAttribute="centerX" secondItem="OIc-cg-9hO" secondAttribute="centerX" id="TrB-hI-7Kw"/>
<constraint firstAttribute="trailing" secondItem="OIc-cg-9hO" secondAttribute="trailing" constant="20" id="yjH-nf-D9U"/>
</constraints>

更多的是解决方法而不是答案:但我在代码中添加了如下约束:

    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:-20.0]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterX
multiplier:1.0
constant:0.0]];

[self addConstraint:[NSLayoutConstraint constraintWithItem:self.cellName
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0
constant:0.0]];

为了让它工作,我需要编码约束和 IB 约束。不知道为什么!

最佳答案

听起来您的 Interface Builder 约束正在使用 iOS7 不支持的“Constraint to Margin”选项。在 IB 中打开约束并检查它们中是否有选中“相对于 margin ”选项的项目。

参见 What is "Constrain to margin" in Storyboard in Xcode 6What is "Constrain to margin" in Storyboard in Xcode 6有关 margin 约束的更多详细信息。

关于ios - AutoLayout 在 ios 8 中有效但在 ios 7 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26171855/

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