1

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

TonyW
  • 18,375
  • 42
  • 110
  • 183
  • May I know what you used as the imageview's content mode? You could probably set different background color for the imageview and the uiview. That way we can know for sure whether the problem is the imageview not filling the uiview or the image not filling its imageview – Dickson Leonard Jul 13 '16 at 03:33
  • @DicksonLeonard, that's a very good point! I am using `Scale To Fill` mode for the UIImageView. I am going to test it with different colors. Thanks – TonyW Jul 13 '16 at 03:35

0 Answers0