Change width to a IBOutlet (UIImageView)

我们两清 提交于 2019-12-12 02:46:36

问题


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?


回答1:


you can just set the width of img

img.frame.size.width = 100


来源:https://stackoverflow.com/questions/32061033/change-width-to-a-iboutlet-uiimageview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!