Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动布局在ios 8中工作,但在ios 7中不工作?_Ios_Objective C_Ios7_Ios8 - Fatal编程技术网

自动布局在ios 8中工作,但在ios 7中不工作?

自动布局在ios 8中工作,但在ios 7中不工作?,ios,objective-c,ios7,ios8,Ios,Objective C,Ios7,Ios8,我正在UICollectionViewCell中使用自动布局。超级简单的布局:只是一个UILabel。我希望UILabel占据整个宽度减去一个20px的插入,并在单元格中垂直和水平居中。我已经设置了一些约束来实现这一点。如果我在任何ios 8设备或模拟器上运行它,它都能完美工作。但是,当我在一些ios 7设备上运行它时,这些约束没有任何效果。我试着翻阅苹果的文档,但它们的变化似乎都与自动布局无关 以下是XML源代码,尽管我怀疑它的意义: <constraints>

我正在UICollectionViewCell中使用自动布局。超级简单的布局:只是一个UILabel。我希望UILabel占据整个宽度减去一个20px的插入,并在单元格中垂直和水平居中。我已经设置了一些约束来实现这一点。如果我在任何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>

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

听起来您的界面生成器约束正在使用iOS7不支持的“约束到边距”选项。打开IB中的约束,并检查其中是否有选中“相对于边距”选项的项目


有关对边距的约束的更多详细信息,请参见和。

这发生在我身上,你必须检查UI中的每个约束,并删除iOS 8引入的、不支持iOS 7的相对边距,不要忘记某些可能会忘记的地方(如UITableView单元格)

要查找此选项,请执行以下操作:

  • 点击UI控件

  • 去找尺码检查员

  • 双击任何约束并检查边距,如果有任何更改,只需将其关闭并修复约束即可


  • 在iOS 7中,您得到了什么结果?在这些约束中,我看不到任何不应该在iOS 7中工作的东西。UILabel具有与xibI完全相同的高度和宽度,具有完全相同的设置和问题。还有什么解决方案吗?在LayoutSubView的代码中放置约束是唯一可行的方法。你会收到一些关于重叠约束的投诉,但它是有效的。你会在iOS 7和iOS 8上使用不同的屏幕尺寸吗?您的一些约束可能没有为某些大小的类安装。我甚至找不到该选项,但xml中没有任何内容
        [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]];