UITableViewCell类布局无法以编程方式运行

时间:2017-02-16 10:57:41

标签: ios iphone uitableview swift3 xcode8

我要问的是,当我以编程方式更改约束时,输出会出错,但如果在故事板输出中更改了相同的约束,则更适合我上传图片。

Change of constraints programmatically and the output is smashed

Change the constraint with same constant '0' in storyboard and o/p is perfect

这是我的代码特定单元格:

@IBOutlet var imageView1: UIImageView!
@IBOutlet var imageView2: UIImageView!
@IBOutlet var imageView3: UIImageView!
@IBOutlet var imageView4: UIImageView!
@IBOutlet var imageView5: UIImageView!

@IBOutlet var bttnImageCover1: UIButton!
@IBOutlet var bttnImageCover2: UIButton!
@IBOutlet var bttnImageCover3: UIButton!
@IBOutlet var bttnImageCover4: UIButton!
@IBOutlet var bttnImageCover5: UIButton!

@IBOutlet var image1HeightCR: NSLayoutConstraint!
@IBOutlet var image1HeightRR: NSLayoutConstraint!
@IBOutlet var image2HeightCR: NSLayoutConstraint!
@IBOutlet var image2HeightRR: NSLayoutConstraint!
@IBOutlet var image3HeightCR: NSLayoutConstraint!
@IBOutlet var image3HeightRR: NSLayoutConstraint!
@IBOutlet var image4HeightCR: NSLayoutConstraint!
@IBOutlet var image4HeightRR: NSLayoutConstraint!
@IBOutlet var image5HeightCR: NSLayoutConstraint!
@IBOutlet var image5HeightRR: NSLayoutConstraint!

/* StoryBoard Outlets End */

var imagesArr               =   [UIImageView]()
var imagesConstraintsArr    =   [NSLayoutConstraint]()
var imagesRRConstraintsArr  =   [NSLayoutConstraint]()
var bttnCoverArr    =   [UIButton]()
var totalImages     =   Int()

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    imagesArr       =   [imageView1,imageView2,imageView3,imageView4,imageView5]
    imagesConstraintsArr    =   [image1HeightCR,image2HeightCR,image3HeightCR,image4HeightCR,image5HeightCR]
    imagesRRConstraintsArr  =   [image1HeightRR,image2HeightRR,image3HeightRR,image4HeightRR,image5HeightRR]

    bttnCoverArr            =   [bttnImageCover1,bttnImageCover2,bttnImageCover3,bttnImageCover4,bttnImageCover5]

}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override func layoutSubviews() {
    print("totalImages in Static cell:\(totalImages)")
    for i in (totalImages..<imagesConstraintsArr.count){
        print("i:\(i)")
        imagesConstraintsArr[i].constant    =   0 **strong text**// This is the devil. I want to change it to 0 to make it disappear but it stretching his upper neighbour view.  
        imagesRRConstraintsArr[i].constant  =   0 **strong text**// Another brother of devil
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在imageView.layoutIfNeeded更新要更新约束的{for循环中的NSLayoutConstraint后触发UIImageViews