Why is my vfl not working?

I have the following code that I am trying to implement a dynamic image added to a vertical scroll view. I would like to have my limitations so that the image is inside the scrollView borders.

However, the results are that the image seems to remain at its original size (which is larger than scrolling, so the image is 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

+5
source share
2 answers

UIView inside a UIScrollView cannot be parent-sized. You can create a UIView inside a UIScrollView and update the width in layoutSubviews and add all the UIView inside that UIView. Here's how you can do it: (To do this, you need to set the contentSize height of the UIScrollView , but you must also set this to do the scrolling.)

 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
source

You need to activate them as shown below, just edit the code for your implementation.

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

Source: https://habr.com/ru/post/1244943/


All Articles