Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is my vfl not working?

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.

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)

    }

}

enter image description here

like image 816
Eran Avatar asked Oct 19 '22 13:10

Eran


1 Answers

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.)

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)
like image 102
Stijn van der Laan Avatar answered Oct 21 '22 06:10

Stijn van der Laan