【发布时间】:2015-07-10 16:30:36
【问题描述】:
嘿,由于某种原因,我正在努力尝试设置我的一个图像视图的高度和宽度。我想设置它,这样高度只占我屏幕的 20%。我知道要定期设置它,您可以执行以下操作: 图像 = (0,0,50,50)
但我需要高度不是静态数字。 像 image = (0,0, frame.height * 0.2, 50)
有什么建议吗?
【问题讨论】:
标签: ios xcode swift uiimageview
嘿,由于某种原因,我正在努力尝试设置我的一个图像视图的高度和宽度。我想设置它,这样高度只占我屏幕的 20%。我知道要定期设置它,您可以执行以下操作: 图像 = (0,0,50,50)
但我需要高度不是静态数字。 像 image = (0,0, frame.height * 0.2, 50)
有什么建议吗?
【问题讨论】:
标签: ios xcode swift uiimageview
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.frame = CGRect(0,0, screenSize.height * 0.2, 50) 不同的是这样看起来更快捷。 CGRectMake 是 Objective-C 风格
使用自动查看
image.heightAnchor.constraint(equalToConstant: CGFloat(8)).isActive = true
【讨论】:
嘿,我很快就知道了。由于某种原因,我只是在放屁。
image.frame = CGRectMake(0 , 0, self.view.frame.width, self.view.frame.height * 0.2)
【讨论】:
更改UIView 的高度和/或宽度的更快/替代方法是通过frame.size 设置其宽度/高度:
let neededHeight = UIScreen.main.bounds.height * 0.2
yourView.frame.size.height = neededHeight
【讨论】:
你可以使用这个代码
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)
【讨论】:
imageView 是主视图的子视图。它是这样工作的
image.frame = CGRectMake(0 , 0, super.view.frame.width, super.view.frame.height * 0.2)
【讨论】: