2016-12-29 69 views
0

我正在通過採取動手方式教授自己斯威夫特。我有一個簡單的圖像,其高度限制在故事板中設置爲150。不過,我想以編程方式將其更改爲70。如何以編程方式更改UIImageView的高度?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let tableElement = tableView.dequeueReusableCell(withIdentifier: "FirstTable", for: indexPath) 
    tableElement.BigImage.image = ?? 
    return tableElement 
} 

我做tableElement.BigImage.image訪問圖像,我想找到它的高度屬性並將其設置爲70。我已經試過這tableElement.BigImage.image.topCapHeight = 70不過是給出了一個錯誤。我怎樣才能做到這一點?

這是我的出路吧

class myNewCell: UITableViewCell { 

    @IBOutlet weak var BigImage: UIImageView! 


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

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

     // Configure the view for the selected state 
    } 

} 

我想學習如何改變圖像的高度編程,因爲我得到的圖像的2個不同批次的,我想他們的高度排序的分離紅色/藍色的東西。

+4

創建的ImageView的身高約束出口,改變約束常數70。同樣,你正在試圖改變形象,而不是ImageView的 –

+0

的高度約束@ New16你不能讓動態細胞的任一網點。 – Lumialxk

+0

@Lumialxk OP可以出口到'UITableViewCell'爲'UIImageView'的高度約束,並相應地更新它'cellForRow' – Rikh

回答

1

雙擊編輯約束。爲它設置標識符。 enter image description here

,並以編程找到它。

for constraint in UIView().constraints { 
    if constraint.identifier == "your identifier" { 
     // set here 
    } 
} 

更新
當你創建一個自定義單元格,添加另一個出口是更好的。 CTRL-DRAG一個插座,它看起來像這樣。

class myNewCell: UITableViewCell { 

    @IBOutlet weak var BigImage: UIImageView! 
    @IBOutlet weak var topCapHeight: NSLayoutConstraint! 

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

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

     // Configure the view for the selected state 
    } 

} 
+0

非常感謝這是我需要的到底是什麼 –