Creating a UIView in an interface builder that automatically centers when added as a preview

I created the UIBiew xib in Interface Builder and tried everything I could to indicate that the UIView should center itself, fix itself in the center, navigate in central coordinates, etc. etc.

But whenever I add it as a subview in the code, I also need to programmatically set its frame with CGRectMake(), otherwise it will always be added to the upper left corner of its parent. The math to redraw it is pointless and ugly, so I guess I just won’t return a bit in IB inspector correctly.

Can anyone confirm this, and if so, what do I need to do in IB to do this?

+3
source share
2 answers

Centering, but saving size in IB is not possible. Still centering, but saving fields to control it.

You will have to redefine the layoutSubviews post or just save the calculation code you wrote.

0
source

Why don't you just set the just added subview object to be a point created by reducing the width and height of the add-in in half?

, , IB ( ), , containerView.bounds( - , x, y 0, .

+1

Source: https://habr.com/ru/post/1731811/


All Articles