Why is there a gap between horizontal scroll content?

I installed a UIScrollView, which contains two ViewController's. Here's what mine looks like viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    scrollView.delegate = self

    self.automaticallyAdjustsScrollViewInsets = false

    let allViewController = self.storyboard!.instantiateViewControllerWithIdentifier("All") as! AllViewController;
    let favoritesViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Favorites") as! FavoritesViewController;

    scrollView!.contentSize = CGSizeMake(2*CGRectGetWidth(allViewController.view.frame), CGRectGetHeight(scrollView!.frame));

    let viewControllers = [allViewController, favoritesViewController]

    var idx:Int = 0;

    for viewController in viewControllers {
        addChildViewController(viewController);
        let originX:CGFloat = CGFloat(idx) * CGRectGetWidth(scrollView!.frame);
        viewController.view.frame = CGRectMake(originX, 0, viewController.view.frame.size.width, viewController.view.frame.size.height);
        scrollView!.addSubview(viewController.view)
        viewController.didMoveToParentViewController(self)
        idx++;
    } 

Everything works fine on the iPhone 6 simulator, but when I run it on the iPhone 5 simulator, there is a gap between the controllers when scrolling:

[ enter image description here] ScrollView has an automatic layout set to 0 for leading, trailing, top, and bottom.

+4
source share
4 answers

If you want to set the origin to viewDidLoad:, and you definitely know that your scroll view will have a screen width, in your calculation logic CGRectGetWidth(scrollView!.frame)use instead

var bounds = UIScreen.mainScreen().bounds
var width = bounds.size.width

to get the correct width before your subprojects have done the layout.

: Github

UPD: , scrollview Autoresize Subviews, UITableViewCell commit github. , . , :

:

enter image description here enter image description here

: SO-

+2

. , . , 4,7- . , viewDidLoad x 375. viewWillAppear didLayoutSubviews, (320 iPhone 5). , , .

+1

, UIScrollView, , :

import UIKit

class MyScrollView: UIScrollView {

    override func layoutSubviews() {
        super.layoutSubviews()
        var idx:Int = 0;
        contentSize = CGSizeMake(2*CGRectGetWidth(bounds), CGRectGetHeight(bounds));
        for sview in self.subviews as! [UIView] {
            let originX:CGFloat = CGFloat(idx) * CGRectGetWidth(bounds);
            sview.frame = CGRectMake(originX, 0, CGRectGetWidth(bounds), CGRectGetHeight(bounds));
            idx++
        }
    }

}

viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    self.automaticallyAdjustsScrollViewInsets = false

    let allViewController = self.storyboard!.instantiateViewControllerWithIdentifier("All") as! AllViewController;
    let favoritesViewController = self.storyboard!.instantiateViewControllerWithIdentifier("Favorites") as! FavoritesViewController;

    for viewController in [allViewController, favoritesViewController] {
        addChildViewController(viewController);
        scrollView!.addSubview(viewController.view)
        viewController.didMoveToParentViewController(self)
    }
}
+1

Add two subviews to your scrollview as placeholders and add constraints between them and scrollview and create two @IBOutlet for placeholders.
In viewDidLoad just set the scroll size, add childViewControlers and add their views to placeholders

0
source

All Articles