2015-04-01 63 views
1

我在界面生成器中創建了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)) 
} 

當我運行應用程序,我得到下面這正是我想要 enter image description here

當我滾動的TableView和時行消失,重新出現(或當我去到另一個的viewController和回來)我得到以下幾點:

enter image description here

我不明白爲什麼。 如果我補充一點:

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)]> 
+0

檢查這種方法:[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

+0

並不能真正幫助作爲對於我來說,我設置了所有的約束條件(我沒有看到哪一個缺失),並且答案中提供的鏈接沒有以編程方式討論約束條件,但是在IB中 – Nico 2015-04-01 11:01:23

+0

嘗試將autosizingMask添加到rowView rowView.autoresizingMask = UIViewAut oresizing。FlexibleWidth – Ram 2015-04-01 11:13:49

回答

0

我後,我發現,因爲它可能會幫助解決。感謝拉姆的幫助。

事實上,當您以編程方式使用自動佈局時,不要忘記在要添加約束的每個組件上添加setTranslatesAutoresizingMaskIntoConstraints(false)

在我的情況下,我在界面生成器中創建了約束條件,並且因爲我需要在運行時從行中刪除UILabel,所以必須重新創建我的行的約束條件(例如寬高比,寬度或高度)。因此,我只想更改我的組件在我的行中的位置,但我不需要重新創建flagImageViewhistoryButton的約束條件,只要我不刪除它們即可。

但我需要從我的rowView中刪除約束,因爲我正在添加新的並刪除一些。這rowView實際上是我的UITableViewCellcontentView。通過消除約束條件,我還刪除了與其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)) 
相關問題