繁体   English   中英

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

[英]Programmatically change the height and width of a UIImageView Xcode Swift

嘿,出于某种原因,我正在努力尝试设置我的一个图像视图的高度和宽度。 我想将其设置为高度仅占屏幕的 20%。 我知道要定期设置它,您可以执行以下操作:image = (0,0,50,50)

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

有什么建议?

Swift 3 中接受的答案:

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

使用自动查看

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

嘿,我很快就想通了。 出于某种原因,我只是在放屁。

image.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.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)

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

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

imageView 是主视图的子视图。 它是这样工作的

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM