0

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?

Fabio Cenni
  • 841
  • 3
  • 16
  • 30

1 Answers1

0

you can just set the width of img

img.frame.size.width = 100
whereisleo
  • 818
  • 5
  • 12
  • I already tried to do this, but I thought that I have to assign the float to the width like this img.frame.width = 100. However if I had to change width and height, I could use img.frame.width == 100 and img.frame.height == 80, it's true? – Fabio Cenni Aug 18 '15 at 09:17
  • Bad answer and even still accepted. Please edit. This is the basics. Append 'size' property img.frame.size.width = 100 (just one equals sign). – pedrouan Sep 02 '16 at 22:02
  • Bad answer? Works fine for me when "img" is actually my UIImageView. Doesn't interfere with with the positioning constraints set in IB, except of course for the width (and height). – Wayne Henderson Mar 31 '18 at 00:39