How to set UIView size to match parent without constraints programmatically
So many answers and nobody is explaining what's wrong.
I will try.
You are setting the frame of newView
to your superviews frame before the autolayout engine has started to determine your superviews position and size. So, when you use the superviews
frame, you are using its initial frame. Which is not correct in most cases.
You have 3 ways to do it correctly:
Use autolayout constraints for your
newView
Set
newView
s frame in theviewDidLayoutSubviews
method. Which iscalled when the autolayout engine finishes determining the frames actual values. (Note: This method can be called multiple times)- Set an autoresizing mask for
newView