mbogh / NibDesignable

Elegant way of enabling IBDesignable on your nib-based views
MIT License
342 stars 74 forks source link

Improper layout #30

Open thebarndog opened 8 years ago

thebarndog commented 8 years ago

I found a bug where if a view inherits from NibDesignable, layoutSubviews gets called with the original sizes from the nibs but not for subsequent resizes. I noticed it when I was making a circular image view and despite doing self.imageView.layer.cornerRadius = self.imageView.frame.size.width /2 in layoutSubviews, the image was rounded strangely, even when it had a 1:1 aspect ratio using autolayout.

My fix was simply to add self.layoutIfNeeded to awakeFromNib but I suspect the issue goes deeper than that.

farzadshbfn commented 8 years ago

Where did you use your view? I had the same results in NibDesignableCollectionReusableView. But the strange thing is, layoutSubviews() does get called, and it get's called with correct bounds, but layer does not change.

farzadshbfn commented 8 years ago

@startupthekid Did you call super.layoutSubviews() ?

thebarndog commented 8 years ago

@farzadshbfn Yeah I did. I'm still not sure what was causing it.

farzadshbfn commented 8 years ago

@startupthekid Before or after your changes? because I digged into it, layoutSubviews gets called immediately after awakeFromNib, with correct bounds, but subviews are not correct yet. So by calling super.layoutSubviews() my problem was solved. P.s: I used my own fork of this project (which i hope to merge it into the main one asap), I don't use the extra view as root of the nibDesignable class. If my solution didn't work for you, try self.subviews[0].setNeedsLayout() in awakeFromNib.