0

I have a scrollView which I instantiate programaticaly. What I want to do is to add constraints so it will look good in both horizontal and vertical orientation. The problem that constraints not working

How it looks vertically - good

enter image description here

How it looks horizontally - not good

enter image description here

Code is following

class FAPhoto: UIViewController, UIScrollViewDelegate {

var imageURLsArray = [String]()
var imageViews:[UIImageView] = []
var arrayOfPhotos = [Photo]()
var scrollView = UIScrollView()

override func viewDidLoad() {
    super.viewDidLoad()
    scrollView = UIScrollView(frame: self.view.frame)
    view.addSubview(scrollView)
    scrollView.contentSize = CGSizeMake(view.frame.size.width * CGFloat(arrayOfPhotos.count), scrollView.frame.size.height)
    scrollView.pagingEnabled = true
    for (var i = 0; i < arrayOfPhotos.count; i++) {
        var imageView = UIImageView()
        imageView.frame = CGRectMake(CGFloat(i) * view.frame.size.width, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)
        imageView.contentMode = .ScaleAspectFit
        let image = imageView.hnk_setImageFromURL(NSURL(string: arrayOfPhotos[i].url!)!)
        scrollView.addSubview(imageView)


    }

}

override func viewWillAppear(animated: Bool) {

}

override func viewWillLayoutSubviews() {
    let bindings = Dictionary(dictionaryLiteral: ("scrollView", self.scrollView))

    let horizontalConstraints =
    NSLayoutConstraint.constraintsWithVisualFormat(
        "H:|-0-[scrollView]-0-|",
        options: [],
        metrics: nil,
        views: bindings)
    self.view.addConstraints(horizontalConstraints)

    let verticalConstraints =
    NSLayoutConstraint.constraintsWithVisualFormat(
        "V:|-0-[scrollView]-0-|",
        options: [],
        metrics: nil,
        views: bindings)
    self.view.addConstraints(verticalConstraints)
}
}
Alexey K
  • 6,537
  • 18
  • 60
  • 118
  • Do you have warnings about broken constraints in the console? As this is a scroll view... are you able to scroll around in either orientation? If so, in which directions for each? – nhgrif Mar 12 '16 at 20:51

1 Answers1

0

The problem is that you set contentSize of scrollView and imageView's frames in viewDidLoad. This is OK for portrait mode, but when view rotates to landscape, it's frame also changes, so you have to update these values accordingly. I think you have 3 opportunities here.

  • Track rotation events and update contentSize of scrollView and imageView's frames, when view rotates.
  • Use Autolayout for scrollView. You will not have to calculate contentSize or set imageView's frames, just set constraints once in viewDidLoad. Here you can find some examples https://stackoverflow.com/a/20232911/4757335.
  • Use UICollectionView instead of UIScrollView. It handles rotation much easier.
Community
  • 1
  • 1
Misternewb
  • 1,066
  • 9
  • 12
  • Thanks for reply! I think I will try with first option. Can you advice, how can I change imageView's frames inside scrollView after they were created ? – Alexey K Mar 12 '16 at 21:03
  • In this case you will have to store references to imageViews in some array and then set their frames, just like you set them in viewDidLoad. Also I updated answer, and strongly recommend you to try third option. – Misternewb Mar 12 '16 at 21:16