Swift:如何调整View Background Image的大小?

时间:2016-06-17 13:36:42

标签: ios swift view image-resizing

我在视图中添加了背景图片。但是当我尝试调整图像大小时,没有任何反应。我知道图像太大了,但我可以用代码制作Scale Aspect Fill。

    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "matchfield.jpg")!)
    self.view.contentMode = .ScaleAspectFill

此代码安静无效。我该如何调整大小?

更新

我在此链接中找到了解决方案:UIView background image resize

问题已经结束。使用背景图像是非常敏感的情况,我不建议这样做。谢谢你回答。

2 个答案:

答案 0 :(得分:1)

使用以下代码调整大小......

var image : UIImageView
image = UIImageView(frame:CGRectMake(0, 0, 200, 150))
image.autoresizingMask = UIViewAutoresizing.FlexibleBottomMargin | UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleRightMargin | UIViewAutoresizing.FlexibleLeftMargin | UIViewAutoresizing.FlexibleTopMargin | UIViewAutoresizing.FlexibleWidth
image.contentMode = UIViewContentMode.ScaleAspectFit

答案 1 :(得分:0)

考虑到Stanly的回答,尝试将autoresizingMask东西放入方括号中并改为使用逗号:

var image : UIImageView
image = UIImageView(frame:CGRectMake(0, 0, 200, 150))
image.autoresizingMask = [UIViewAutoresizing.FlexibleBottomMargin, UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleRightMargin, UIViewAutoresizing.FlexibleLeftMargin, UIViewAutoresizing.FlexibleTopMargin, UIViewAutoresizing.FlexibleWidth]
image.contentMode = UIViewContentMode.ScaleAspectFit