user023425
user023425

Reputation: 327

Xib frame size isn't correct when adding it as a subview

I have a xib that looks like this:

enter image description here

Here is my class for Video Container View:

class VideoContainerView: UIView {

    @IBOutlet var rootView: UIView!
    @IBOutlet weak var videoView: VideoView!
    @IBOutlet weak var playIconContainer: UIView!
    @IBOutlet weak var timeContainer: UIView!
    @IBOutlet weak var timeLabel: UILabel!

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

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

    func initialize() {
        Bundle.main.loadNibNamed("VideoContainerView", owner: self, options: nil)
        rootView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        addSubview(rootView)
    }

}

And here is my class for PostMediaCollectionViewCell:

class PostMediaCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var container: UIView!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet weak var moreView: UIView!
    @IBOutlet weak var moreViewLabel: UILabel!
    @IBOutlet weak var videoContainerView: VideoContainerView!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    func initialize() {

    }

}

But when I run the app in the simulator, the VideoContainerView width/height is wrong. It doesn't seem to respect my auto layout values for it, because after printing out its bounds, its width and height equal 661.0 and 372.0 (the hard-coded values in the xib, screenshot above).

What am I doing wrong and what do I need to change to get this to work?

Upvotes: 0

Views: 845

Answers (3)

Jaydip Finava
Jaydip Finava

Reputation: 67

let view = Bundle.main.loadNibNamed("VideoContainerView", owner: self, options: nil)?.first as! UIView
addSubview(view)
rootView.frame = self.bounds
rootView.autoresizingMask = [.flexibleWidth,.flexibleHeight] 

Upvotes: 1

Maks
Maks

Reputation: 322

You can setup x, y, hight and width parameters for rootView like this:

rootView.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)

Upvotes: 0

matt
matt

Reputation: 535140

You are failing to size the video container view in the first place:

    Bundle.main.loadNibNamed("VideoContainerView", owner: self, options: nil)
    rootView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    addSubview(rootView)

You need to add some sort of initial sizing, such as

    rootView.frame = self.view.bounds // or whatever

Otherwise, your autoresizing mask start with the size that came out of the xib file, which is almost certainly going to be wrong.

(By the way, your question says "it doesn't seem to respect my auto layout value for it", but in your code, you have no auto layout values at all! If that's what you wanted, you should have used auto layout. autoresizingMask is the opposite of auto layout.)

Upvotes: 0

Related Questions