以编程方式更改 UIImageView 的高度和宽度 Xcode Swift
Programmatically change the height and width of a UIImageView Xcode Swift
嘿,出于某种原因,我正在努力尝试设置我的一个图像视图的高度和宽度。我想将其设置为高度仅占屏幕的 20%。我知道要定期设置它,您可以执行以下操作:
图片 = (0,0,50,50)
但我需要高度不是静态数字。
像 image = (0,0, frame.height * 0.2, 50)
有什么建议吗?
嘿,我很快就想通了。出于某种原因,我只是在放屁。
image.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.2)
let screenSize: CGRect = UIScreen.mainScreen().bounds
image.frame = CGRectMake(0,0, screenSize.height * 0.2, 50)
你可以使用这个代码
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)
Swift3 中接受的答案:
let screenSize: CGRect = UIScreen.main.bounds
image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)
imageView 是主视图的子视图。它是这样工作的
image.frame = CGRectMake(0 , 0, super.view.frame.width, super.view.frame.height * 0.2)
使用自动查看
image.heightAnchor.constraint(equalToConstant: CGFloat(8)).isActive = true
更改 UIView
的高度 and/or 宽度的更快/替代方法是通过 frame.size
:
设置其 width/height
let neededHeight = UIScreen.main.bounds.height * 0.2
yourView.frame.size.height = neededHeight
嘿,出于某种原因,我正在努力尝试设置我的一个图像视图的高度和宽度。我想将其设置为高度仅占屏幕的 20%。我知道要定期设置它,您可以执行以下操作: 图片 = (0,0,50,50)
但我需要高度不是静态数字。 像 image = (0,0, frame.height * 0.2, 50)
有什么建议吗?
嘿,我很快就想通了。出于某种原因,我只是在放屁。
image.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.2)
let screenSize: CGRect = UIScreen.mainScreen().bounds
image.frame = CGRectMake(0,0, screenSize.height * 0.2, 50)
你可以使用这个代码
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)
Swift3 中接受的答案:
let screenSize: CGRect = UIScreen.main.bounds
image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)
imageView 是主视图的子视图。它是这样工作的
image.frame = CGRectMake(0 , 0, super.view.frame.width, super.view.frame.height * 0.2)
使用自动查看
image.heightAnchor.constraint(equalToConstant: CGFloat(8)).isActive = true
更改 UIView
的高度 and/or 宽度的更快/替代方法是通过 frame.size
:
let neededHeight = UIScreen.main.bounds.height * 0.2
yourView.frame.size.height = neededHeight