0
votes

I need to create a view with a scroll view and a page control in it, and place 7 views inside scroll view. To lay out subviews inside the scroll view I use pure Auto layout Approach, that is described here.

So I have my controller with XIB file (I don't use storyboards here) that is pretty simple: it's a UIScrollView and UIPageControl with all constraints set up.

And I have a XIB for a UIView subclass Slide which has 2 UIImageViews and 1 UILabel, and there's also some constraints.

To add some views to UIScrollView I use this code in viewDidLayoutSubviews():

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
    var pSlide: Slide?
    for var i = 0; i < 7; i++ {
        var slide = Slide(frame: self.view.bounds, imageName: "slide-\(i+1)-bg", text: NSLocalizedString("slides_\(i+1)", comment: ""))
        slide.setTranslatesAutoresizingMaskIntoConstraints(false)
        scrollView.addSubview(slide)


        var dict: [NSObject : AnyObject] = ["currentSlide" : slide]
        if let previousSlide = pSlide {
            dict["previousSlide"] = previousSlide
            let constraintsHorizontal = NSLayoutConstraint.constraintsWithVisualFormat("H:[previousSlide][currentSlide]", options: nil, metrics: nil, views: dict)
            scrollView.addConstraints(constraintsHorizontal)

            let constraintsVertical = NSLayoutConstraint.constraintsWithVisualFormat("V:|[currentSlide]|", options: nil, metrics: nil, views: dict)
            scrollView.addConstraints(constraintsVertical)
        } else {
            let constraintsVertical = NSLayoutConstraint.constraintsWithVisualFormat("V:|[currentSlide]|", options: nil, metrics: nil, views: dict)
            scrollView.addConstraints(constraintsVertical)

            let constraintsLeft = NSLayoutConstraint.constraintsWithVisualFormat("H:|[currentSlide]", options: nil, metrics: nil, views: dict)
            scrollView.addConstraints(constraintsLeft)
        }

        if i == 6 {
            let constraintsRight = NSLayoutConstraint.constraintsWithVisualFormat("H:[currentSlide]|", options: nil, metrics: nil, views: dict)
            scrollView.addConstraints(constraintsRight)
        }

        pSlide = slide
    }

    pageControl.numberOfPages = numberOfSlides
    view.layoutSubviews()
}

In this piece of code I create a Slide instance, and set all necessary constraints to it, according to pure Auto Layout approach.

init() method of the Slide class looks like this:

init(frame: CGRect, imageName: String, text: String) {
    super.init(frame: frame)
    NSBundle.mainBundle().loadNibNamed("Slide", owner: self, options: nil)
    self.addSubview(self.view)

    self.view.frame = frame
    self.layoutIfNeeded()

    println("Frame is \(frame); view.frame is \(self.view.frame)")

    backgroundImage.image = UIImage(named: imageName)
    textLabel.text = text

}

I hoped that

self.view.frame = frame
self.layoutIfNeeded()

will help me but no. The problem is, on 3.5 inch screen all my UIScrollView subviews have the height of 568, which is the normal height for 4 inch display, but not for 3.5 inch.

I'm checking the height in viewDidAppear(animated:) method. But, in init() method of Slide class the height appears to be ok — 480.

I'm trying to solve it for second day already, and still nothing works. I know that this may be much more simple to implement without using Auto Layout and Interface Builder, but I need to do it with these.

1
This has nothing to do with your problem, but it looks like you are reinventing UIPageViewController. Before you spend another day you should check that class out.Matthias Bauch
how do you define contentsize? note that with autolayout the contentsize is defined from constraints!Sulthan
@MatthiasBauch post your comment as an answer please. I should always check new stuff in each iOS version.Randex

1 Answers

0
votes

I used UIPageViewController instead of all this mess, and it works just fine.