以编程方式更改UIImageView Xcode Swift的高度和宽度

时间:2015-07-10 16:30:37

标签: ios xcode swift uiimageview

嘿,由于某种原因,我正在努力设置我的一个图像视图的高度和宽度。我想设置它,因此高度仅为我屏幕的20%。我知道定期设置它可以做以下事情: image =(0,0,50,50)

但我需要高度不是静态数字。 像image =(0,0,frame.height * 0.2,50)

之类的东西

有什么建议吗?

7 个答案:

答案 0 :(得分:39)

Swift 3中接受的答案:

let screenSize: CGRect = UIScreen.main.bounds
image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)

答案 1 :(得分:21)

let screenSize: CGRect = UIScreen.mainScreen().bounds
image.frame = CGRectMake(0,0, screenSize.height * 0.2, 50)

答案 2 :(得分:4)

嘿,我不久后想出来了。出于某种原因,我只是一个大脑放屁。

image.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.2)

答案 3 :(得分:2)

你可以使用这段代码

var imageView = UIImageView(image: UIImage(name:"imageName"));
imageView.frame = CGrectMake(x,y imageView.frame.width*0.2,50);

var imageView = UIImageView(frame:CGrectMake(x,y, self.view.frame.size.width *0.2, 50)

答案 4 :(得分:1)

使用自动查看

image.heightAnchor.constraint(equalToConstant: CGFloat(8)).isActive = true

答案 5 :(得分:1)

更改UIView的高度和/或宽度的更快/替代方法是通过frame.size设置其宽度/高度:

let neededHeight = UIScreen.main.bounds.height * 0.2
yourView.frame.size.height = neededHeight

答案 6 :(得分:0)

imageView是主视图的子视图。它以这种方式工作

{{1}}
相关问题