【发布时间】:2015-08-17 22:58:18
【问题描述】:
我正在用 Xcode 做一些实验,我想到了一个小问题:这就是我如何更改由故事板创建的 UIImage 的大小。在我使用的代码下方:
@IBOutlet var label: UILabel!
@IBOutlet var img: UIImageView!//x=0, y=0, w=50, h=50, red, created in the storyboard
var numero = 11
override func viewDidLoad() {
super.viewDidLoad()
if numero < 10
{
label.text = "\(numero)"
img.backgroundColor = UIColor.blueColor()
}
else if numero > 10
{
label.text = "\(numero)"
img.frame = CGRectMake(0, 0, 100, 50)
img.backgroundColor = UIColor.greenColor()
}
}
所以图像改变颜色但不改变宽度。我该如何解决这个问题?但是,如果我以编程方式创建 UIImageView 它可以完美运行:
@IBOutlet var label: UILabel!
var img: UIImageView!
var numero = 11
override func viewDidLoad() {
super.viewDidLoad()
img = UIImageView(frame: CGRectMake(0, 0, 50, 50))
self.view.addSubview(img)
if numero < 10
{
label.text = "\(numero)"
img.backgroundColor = UIColor.blueColor()
}
else if numero > 10
{
label.text = "\(numero)"
img.frame = CGRectMake(0, 0, 100, 50)
img.backgroundColor = UIColor.greenColor()
}
}
有人可以解释一下 UIImageView 的行为在编程方式或情节提要中的区别吗?
【问题讨论】:
标签: swift uiimageview iboutlet cgrect programmatically-created