为什么我的vfl无法正常工作?

时间:2022-07-20 06:50:49

I have the following code which I am trying to implement a dynamic image added to a vertical scroll view. I would like to have its constraints so the image sets itself inside the borders of the scrollView.

我有以下代码,我试图实现添加到垂直滚动视图的动态图像。我想有它的约束,所以图像设置在scrollView的边框内。

However the results are that the image seems to remain in its original size (which is bigger then the scrollview so the image gets cropped)

然而结果是图像似乎保持原始大小(比滚动视图大,因此图像被裁剪)

Here is the code:

这是代码:

@IBOutlet weak var myScrollView: UIScrollView!
private let lettersModel:LettersModel = LettersModel();
private var imgs = [UIImageView]();

override func viewDidLoad() {
    super.viewDidLoad()

    myScrollView.backgroundColor = UIColor.brownColor()

    for var index=0; index<1; index++
    {

        let myImage:UIImage = UIImage(named: lettersModel.getLetterAt(index))!
        let myImageView:UIImageView = UIImageView()
        myImageView.image = myImage
        myImageView.contentMode = UIViewContentMode.ScaleAspectFit

        myScrollView.addSubview(myImageView)

        myImageView.translatesAutoresizingMaskIntoConstraints = false;

        myScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImageView]-|", options: [], metrics: nil, views:["myImageView":myImageView] ))

        imgs.append(myImageView)

    }

}

为什么我的vfl无法正常工作?

2 个解决方案

#1


2  

UIView's inside a UIScrollView can't size to it's parent. You can create a UIView inside the UIScrollView and update the width in layoutSubviews and add all UIView inside that UIView. Here is how you can do that: (For this to work you need to set the height of the contentSize of the UIScrollView but you also have to set this in order to make it scroll.)

在UIScrollView中的UIView无法调整它的父级大小。您可以在UIScrollView中创建UIView并更新layoutSubviews中的宽度,并在UIView中添加所有UIView。以下是如何执行此操作:(为此,您需要设置UIScrollView的contentSize的高度,但您还必须设置它以使其滚动。)

class MyScrollView : UIScrollView {

    let contentView = UIView()

    override init (frame : CGRect) {
        super.init(frame : frame)
        addSubview(contentView)
    }

    convenience init () {
        self.init(frame:CGRect.zero)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }

    override func layoutSubviews() {
        contentView.frame.size.width = frame.width
        contentView.frame.size.height = contentSize.height
        super.layoutSubviews()
    }
}

var myScrollView = MyScrollView(frame: CGRect(x: 0, y: 0, width: 400, height: 600))
var imgs = [UIImageView]();

let myImage = UIImage(named: "achtergrond")
let myImageView:UIImageView = UIImageView()
myImageView.image = myImage
myImageView.contentMode = UIViewContentMode.ScaleAspectFit

myScrollView.contentSize.height = 1000 // <-- setting the contentSize
myScrollView.contentView.addSubview(myImageView) // <-- You add the UIImageView to the contentView

myImageView.translatesAutoresizingMaskIntoConstraints = false;
myScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImageView]-|", options: [], metrics: nil, views:["myImageView":myImageView] ))

imgs.append(myImageView)

#2


1  

You have to activate them like below, just edit code for your implementation.

您必须像下面一样激活它们,只需为您的实现编辑代码。

var allConstraints = [NSLayoutConstraint]()
var constraintName = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImageView]-|", options: [], metrics: nil, views:["myImageView":myImageView])
NSLayoutConstraint.activateConstraints(allConstraints)

#1


2  

UIView's inside a UIScrollView can't size to it's parent. You can create a UIView inside the UIScrollView and update the width in layoutSubviews and add all UIView inside that UIView. Here is how you can do that: (For this to work you need to set the height of the contentSize of the UIScrollView but you also have to set this in order to make it scroll.)

在UIScrollView中的UIView无法调整它的父级大小。您可以在UIScrollView中创建UIView并更新layoutSubviews中的宽度,并在UIView中添加所有UIView。以下是如何执行此操作:(为此,您需要设置UIScrollView的contentSize的高度,但您还必须设置它以使其滚动。)

class MyScrollView : UIScrollView {

    let contentView = UIView()

    override init (frame : CGRect) {
        super.init(frame : frame)
        addSubview(contentView)
    }

    convenience init () {
        self.init(frame:CGRect.zero)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("This class does not support NSCoding")
    }

    override func layoutSubviews() {
        contentView.frame.size.width = frame.width
        contentView.frame.size.height = contentSize.height
        super.layoutSubviews()
    }
}

var myScrollView = MyScrollView(frame: CGRect(x: 0, y: 0, width: 400, height: 600))
var imgs = [UIImageView]();

let myImage = UIImage(named: "achtergrond")
let myImageView:UIImageView = UIImageView()
myImageView.image = myImage
myImageView.contentMode = UIViewContentMode.ScaleAspectFit

myScrollView.contentSize.height = 1000 // <-- setting the contentSize
myScrollView.contentView.addSubview(myImageView) // <-- You add the UIImageView to the contentView

myImageView.translatesAutoresizingMaskIntoConstraints = false;
myScrollView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImageView]-|", options: [], metrics: nil, views:["myImageView":myImageView] ))

imgs.append(myImageView)

#2


1  

You have to activate them like below, just edit code for your implementation.

您必须像下面一样激活它们,只需为您的实现编辑代码。

var allConstraints = [NSLayoutConstraint]()
var constraintName = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[myImageView]-|", options: [], metrics: nil, views:["myImageView":myImageView])
NSLayoutConstraint.activateConstraints(allConstraints)