2015-07-12 24 views
0

例如:如何通過代碼調整UIView的大小?

我可以通過頂部和底部約束得到UIImageView(identifier:ImgView)的高度,我想讓它的寬度爲(4/3 x高度)。

我該怎麼辦?

我曾經嘗試過用這句話做:

ImgView.frame.width = ImgView.frame.height * 4.0/3.0 

但它並沒有一個錯誤的工作說:

it can't assign to ImgView.frame.width 

那麼,如何才能做到這一點?

回答

1

試試這個:

CGRect frame = ImgView.frame; 
frame.size.width = frame.size.height * 4/3; 
ImgView.frame = frame; 

或者一個班輪:

ImgView.frame = CGRectMake(ImgView.frame.origin.x, 
          ImgView.frame.origin.y, 
          ImgView.frame.size.height * 4/3, 
          ImgView.frame.size.height); 
+0

好的,今晚我會試試。感謝您的答案:D – Hashira

+0

這應該工作,如果你在代碼中做,但如果你想要一個更好的解決方案,請使用[@Poq's解決方案](http://stackoverflow.com/a/31365841/2177402)使用**長寬比**約束。 –

2

使用的比例限制。十分簡單。

enter image description here