2016-09-26 31 views
0

这是我目前的状况:在我的UIImageView中约束有什么问题,为什么我不能在我的UIViewController上拉伸图像?

enter image description here

我有一个UIViewControllerUIImageView就可以了。

后者具有以下限制:trailing to superviewleading to superviewtop space to top layout guidewidth equals 320height equals 820

高度和宽度连为网点上我的课,我设定他们viewDidLoad

@IBOutlet weak var imageHeightconstraint: NSLayoutConstraint! 

@IBOutlet weak var imageWidthConstraint: NSLayoutConstraint! 

@IBOutlet weak var backgroundImage: UIImageView! 

override viewDidLoad(){ 
    backgroundImage.af_setImageWithURL(NSURL(string: photoURL)!) 
    imageHeightconstraint.constant = backgroundImage.image!.size.height 
    imageWidthConstraint.constant = self.view.frame.size.width 
} 

现在,我的形象是这样的:

enter image description here

是480x640px照片。我试图在我的故事板中将模式设置为centerscale to fill,aspect fill,aspect fittop

例如,这是center

enter image description here

aspect fit

enter image description here

aspect fill

enter image description here

但我不知道如何从左到右拉伸此图像并保持纵横比,因此整张照片都可见,其左右边缘粘在屏幕的左侧和右侧边缘。 我认为这两个约束可以解决它:

imageHeightconstraint.constant = backgroundImage.image!.size.height 
imageWidthConstraint.constant = self.view.frame.size.width 

但它不起作用。也许问题是照片的分辨率很低(480x640),所以高度只有那么小?你能给我任何提示我怎么修复它?

回答

1

要使图像棒屏幕两侧和保持纵横比,设定这4个约束:

  1. 领先的ImageView的边沿到领导的SuperView的边缘。
  2. ImageView的尾部边缘到SuperView的尾部边缘。
  3. ImageView顶边到顶部布局指南底部。
  4. 设置宽高比约束:ImageView Width等于ImageView高度,使用multiplier480:640。要设置此,控制 -drag对角线在ImageView中,并从弹出窗口中选择长宽比。然后将乘数更改为480:640

    aspect ratio constraint

设置的图像Scale to fill内容模式。

1

您可能不会呼叫setNeedsLayout(),然后layoutIfNeeded()

为了让UI更新,您需要告诉视图更新,而不仅仅是更改约束。无论哪种方式,如果您希望imageView始终如一地填满屏幕,并且ImageView上不应该有高度/宽度,只需要拥抱两侧。同时确保这些限制忽略了利润。

如果您希望图像填充imageView,请将调整大小模式设置为aspectFill,并且如果您要调整大小,直到左/右或顶部/底部到达视图的边缘,则aspectFit就是你需要。

相关问题