我在界面生成器中創建了UITableView。裏面是動態細胞。我說這樣的限制(此方法被調用tableView:cellForRowAtIndexPath
):啓動應用程序和應用程序運行時UITableView中的自動佈局結果會有所不同
@IBOutlet weak var rowView: UIView!
@IBOutlet weak var flagImageView: UIImageView!
@IBOutlet weak var computedRateLabel: UILabel!
@IBOutlet weak var historyButton: UIButton!
func buildConstraintsFor() {
let viewsDictionary = ["rowView": rowView, "flagImageView": flagImageView, "view1": computedRateLabel, "historyButton": historyButton]
rowView.layer.borderWidth = 1
flagImageView.layer.borderWidth = 1
computedRateLabel.layer.borderWidth = 1
historyButton.layer.borderWidth = 1
rowView.layer.borderColor = UIColor.blackColor().CGColor
flagImageView.layer.borderColor = UIColor.purpleColor().CGColor
computedRateLabel.layer.borderColor = UIColor.orangeColor().CGColor
historyButton.layer.borderColor = UIColor.greenColor().CGColor
// remove constraints
rowView.removeConstraints(rowView.constraints())
historyButton.removeConstraints(historyButton.constraints())
// add constraints
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[flagImageView]-[view1]-[historyButton]-|", options: nil, metrics: nil, views: viewsDictionary))
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[flagImageView]-1-|", options: nil, metrics: nil, views: viewsDictionary))
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[view1]-1-|", options: nil, metrics: nil, views: viewsDictionary))
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-1-[historyButton]-1-|", options: nil, metrics: nil, views: viewsDictionary))
// size
historyButton.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[historyButton(==25)]", options: nil, metrics: nil, views: viewsDictionary))
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[rowView(<=68)]", options: nil, metrics: nil, views: viewsDictionary))
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[rowView(>=67)]", options: nil, metrics: nil, views: viewsDictionary))
flagImageView.addConstraint(NSLayoutConstraint(item: flagImageView, attribute: .Width, relatedBy: .Equal, toItem: flagImageView, attribute: .Height, multiplier: 1, constant: 0))
}
當我運行應用程序,我得到下面這正是我想要
當我滾動的TableView和時行消失,重新出現(或當我去到另一個的viewController和回來)我得到以下幾點:
我不明白爲什麼。 如果我補充一點:
let width = UIScreen.mainScreen().applicationFrame.size.width
rowView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[rowView(==\(width))]", options: nil, metrics: nil, views: viewsDictionary))
這似乎是解決這個問題,但是當我去的風景不工作了。
當我做了佈局時,我做錯了嗎?
編輯:
當我在這裏加入他們後打印的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)]>
檢查這種方法:[http://stackoverflow.com/questions/29231390/uitableview-strange-layout-behavior-changes-on-scroll/29327082#29327082 ] [1] [1]:http://stackoverflow.com/questions/29231390/uitableview-strange-layout-behavior-changes-on-scroll/29327082#29327082 – Ram 2015-04-01 10:41:37
並不能真正幫助作爲對於我來說,我設置了所有的約束條件(我沒有看到哪一個缺失),並且答案中提供的鏈接沒有以編程方式討論約束條件,但是在IB中 – Nico 2015-04-01 11:01:23
嘗試將autosizingMask添加到rowView rowView.autoresizingMask = UIViewAut oresizing。FlexibleWidth – Ram 2015-04-01 11:13:49