Swift in iOS 6 Plus simulator: Custom UIView/ImageView cannot fill the top of the container view

127 Views Asked by At

I am trying to create a custom UIView that uses a .xib file, in which a UIImageView was dragged in the xib for displaying pictures. I have the following UIView subclass, however, the custom view/image view cannot fill the very top of the ViewController's superview, and I make sure the auto layout constraints are correct.

import UIKit

@IBDesignable class CustomMotionView: UIView {

    @IBOutlet var contentView: UIView!

    @IBOutlet weak var imageContainerView: UIImageView!
    var imageArray: [UIImage] = []

    init(imageArray: [UIImage]) {
        super.init(frame: CGRectNull)
        xibSetup()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    private func xibSetup() {
        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: String(self.dynamicType), bundle: bundle)
        contentView = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.FlexibleWidth, .FlexibleTopMargin]
        imageContainerView.autoresizingMask = [.FlexibleWidth, .FlexibleTopMargin]
        self.addSubview(contentView)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        let p = (touch?.locationInView(self))!
        print("touches began!, point: \(p))") // this can provide hints for the boundaries of the CustomMotionView in its superview
    }
}

In the storyboard with a UIViewController, I dragged a UIView rectangle and assigned the CustomMotionView to its class. I set auto layout constraints to be: 0 (top), -20 (left), 0 (bottom), and -20 (right). The above touchesBegan() method suggests that the CustomMotionView's layout is inflating fine in its container.

For the UIImageView dragged in the custom UIView's xib file, I make its auto layout constraints: (0,0,0,0). So it appears to me that the UIImageView in the xib cannot fill its superview's container (the UIViewController's view), I was wondering how to fix it. Please see the portrait view attached from my iOS 6 Plus simulator portrait view (but iOS 6 Plus simulator works fine with the landscape view mode; iOS 5S simulator works fine for both portrait and landscape view)

white space in the top part of the container's view

0

There are 0 best solutions below