Ios 启动应用程序和运行应用程序时,UITableView中自动布局的结果不同

Ios 启动应用程序和运行应用程序时,UITableView中自动布局的结果不同,ios,uitableview,swift,autolayout,Ios,Uitableview,Swift,Autolayout,我在Interface Builder中创建了一个UITableView。里面是充满活力的细胞。我添加了如下约束,该方法在tableView:CellForRowatineXpath中调用: 它似乎解决了问题,但当我去看风景时,它不再起作用了 我做布局的时候做错什么了吗 编辑: 当我在添加rowView的所有约束后打印它们的描述时,我得到的是: <NSLayoutConstraint:0x17009f3b0 UIImageView:0x1701f5a00.leading == UITabl

我在Interface Builder中创建了一个UITableView。里面是充满活力的细胞。我添加了如下约束,该方法在tableView:CellForRowatineXpath中调用:

它似乎解决了问题,但当我去看风景时,它不再起作用了

我做布局的时候做错什么了吗

编辑:

当我在添加rowView的所有约束后打印它们的描述时,我得到的是:

<NSLayoutConstraint:0x17009f3b0 UIImageView:0x1701f5a00.leading == UITableViewCellContentView:0x1701968c0.leadingMargin>
<NSLayoutConstraint:0x17009f6d0 H:[UIImageView:0x1701f5a00]-(NSSpace(8))-[UILabel:0x13760f7f0'12 106,98']>
<NSLayoutConstraint:0x17009f9a0 H:[UILabel:0x13760f7f0'12 106,98']-(NSSpace(8))-[UIButton:0x137634b60]>
<NSLayoutConstraint:0x17009dec0 UITableViewCellContentView:0x1701968c0.trailingMargin == UIButton:0x137634b60.trailing>
<NSLayoutConstraint:0x170098920 V:|-(1)-[UIImageView:0x1701f5a00]   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x17009a4a0 V:[UIImageView:0x1701f5a00]-(1)-|   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x174098dd0 V:|-(1)-[UILabel:0x13760f7f0'12 106,98']   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x174099550 V:[UILabel:0x13760f7f0'12 106,98']-(1)-|   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x170283c00 V:|-(1)-[UIButton:0x137634b60]   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x1702837a0 V:[UIButton:0x137634b60]-(1)-|   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x170283d90 V:[UITableViewCellContentView:0x1701968c0(<=68)]>
<NSLayoutConstraint:0x170283de0 V:[UITableViewCellContentView:0x1701968c0(>=67)]>

我发布了我找到的解决方案,因为它可能会有所帮助。谢谢你的帮助

事实上,当您以编程方式使用自动布局时,不要忘记在要向其添加约束的每个组件上添加setTranslatesAutoresizingMaskIntoConstraintsfalse

在我的例子中,我已经在Interface Builder中创建了约束,因为我需要在运行时从行中删除UILabel,所以我必须为行重新创建约束,例如纵横比、宽度或高度。因此,由于我只想更改组件在行中的位置,只要不删除它们,就不需要重新创建flagImageView或historyButton的约束

但在添加新约束和删除一些约束时,我需要从我的行视图中删除这些约束。此行视图实际上是我的UITableViewCell的contentView。通过删除约束,我也删除了链接到其superView(实际上是UITableViewCell本身)的约束。因此,我的contentView与其父视图的大小不同。所以我只需要添加这两行:

rowView.superview?.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[rowView]-0-|", options: nil, metrics: nil, views: viewsDictionary))
rowView.superview?.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[rowView]-0-|", options: nil, metrics: nil, views: viewsDictionary))

检查此方法:[[1]:对我来说没什么帮助,我设置了所有的约束,我看不出缺少哪一个约束,答案中提供的链接没有以编程方式讨论约束,而是在IB中尝试将autosizingMask添加到rowView rowView.autoresizingMask=UIViewAutoresizing.FlexibleWidthNo,它不会更改任何内容,对其他布局(如作为图像、标签和按钮,还可以通过Xcode删除任何自动添加的约束
<NSLayoutConstraint:0x17009f3b0 UIImageView:0x1701f5a00.leading == UITableViewCellContentView:0x1701968c0.leadingMargin>
<NSLayoutConstraint:0x17009f6d0 H:[UIImageView:0x1701f5a00]-(NSSpace(8))-[UILabel:0x13760f7f0'12 106,98']>
<NSLayoutConstraint:0x17009f9a0 H:[UILabel:0x13760f7f0'12 106,98']-(NSSpace(8))-[UIButton:0x137634b60]>
<NSLayoutConstraint:0x17009dec0 UITableViewCellContentView:0x1701968c0.trailingMargin == UIButton:0x137634b60.trailing>
<NSLayoutConstraint:0x170098920 V:|-(1)-[UIImageView:0x1701f5a00]   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x17009a4a0 V:[UIImageView:0x1701f5a00]-(1)-|   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x174098dd0 V:|-(1)-[UILabel:0x13760f7f0'12 106,98']   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x174099550 V:[UILabel:0x13760f7f0'12 106,98']-(1)-|   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x170283c00 V:|-(1)-[UIButton:0x137634b60]   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x1702837a0 V:[UIButton:0x137634b60]-(1)-|   (Names: '|':UITableViewCellContentView:0x1701968c0 )>
<NSLayoutConstraint:0x170283d90 V:[UITableViewCellContentView:0x1701968c0(<=68)]>
<NSLayoutConstraint:0x170283de0 V:[UITableViewCellContentView:0x1701968c0(>=67)]>
rowView.superview?.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[rowView]-0-|", options: nil, metrics: nil, views: viewsDictionary))
rowView.superview?.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[rowView]-0-|", options: nil, metrics: nil, views: viewsDictionary))