将宽度更改为 IBOutlet (UIImageView) [英] Change width to a IBOutlet (UIImageView)

查看:34
本文介绍了将宽度更改为 IBOutlet (UIImageView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用 Xcode 做一些实验时,我想到了一个小问题:这就是我如何改变故事板创建的 UIImage 的大小.在我使用的代码下方:

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

所以图像会改变颜色但不会改变宽度.我怎么能解决这个问题?但是,如果我以编程方式创建 UIImageView,它可以完美运行:

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

有人可以向我解释以编程方式或在故事板中制作的 UIImageView 行为之间的差异吗?

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

推荐答案

你可以只设置img的宽度

you can just set the width of img

img.frame.size.width = 100

这篇关于将宽度更改为 IBOutlet (UIImageView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆