Fabio Cenni Fabio Cenni - 2 months ago 24
Swift Question

Change width to a IBOutlet (UIImageView)

I was doing some experiments with Xcode an a little question comes to my mind: that is how I could change size of an UIImage created by storyboard. below the code I used:

@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()
}
}


So the image changes color but does not change width. How could I fix the problem? However if I create programmatically a UIImageView it works perfectly:

@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()
}
}


Can someone explain me the the differences between the behavior of the UIImageView made programmatically or in the storyboard?

Answer

you can just set the width of img

img.frame.size.width = 100
Comments