Pete
Pete

Reputation: 11

Swift - Child UIView in UIScrollview not showing until after rotation

I've come across some odd behaviour when building a basic UIScrollView example with Auto Layout.

In the Storyboard editor I added a UIScrollView as a child of the default UIView and within that I have added a new UIView and named it contentView:

View

--scrollView

----contentView

I added some labels and text boxes to the contentView and set their constraints to the parent container (contentView).

I also set constraints on contentView to the UIScrollView, the UIScrollView to it's parent and finally set contentView to use the same height & width as the top level UIView. After adding the constraints all errors or warnings about missing constraints are gone and the screen looks exactly how I want it in the Storyboard editor.

For ease of troubleshooting I coloured the primary View blue, the UIScrollView yellow and the contentView green.

When I run the app on the simulator or a real phone the text fields are shrunk down instead of being 200 wide, label 3 and its text box are missing (off screen most likely) and the screen is yellow indicating the UIScrollView is being shown. As soon as I rotate the screen everything looks exactly like it should:

Gifycat image link as I can't post images yet

Printing out the widths of the frames:

I've replicated this behaviour in Swift 1 & 2 using Xcode 6.4 and 7.0 beta 4.

Upvotes: 1

Views: 1609

Answers (1)

Pete
Pete

Reputation: 11

So I managed to fix the child UIView by overriding the viewDidLayoutSubviews() function in viewController.swift

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        let widthConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: parentView.frame.width)

        contentView.addConstraint(widthConstraint)

        let heightConstraint = NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: parentView.frame.height)
        contentView.addConstraint(heightConstraint)
    }

parentView being an outlet for the top level UIView and contentView an outlet for the child UIView within the UIScrollView.

Upvotes: 0

Related Questions