The problem sounds easy but it is making me crazy.
I've created a white view in IB that's called iBag and by constraints it's size depends on screen size.
Now I want create a new UIView programmatically and add as subview to iBag with same size and position by this code
let newView = UIView()
newView.frame = (frame: CGRect(x: 0, y: 0, width: iBag.frame.width, height: iBag.frame.height))
newView.backgroundColor = UIColor.redColor()
So many answers and nobody explain whats wrong.
I will try.
You setting up frame of your
newView by superview frame before the autolayout engine start work to determine views position. So, when you use frame of
superview, you use initial frame, that not correct in most cases. You have 3 ways to do right thing: use autolayout (constraints) for your
newView, OR set up frames for
viewDidLayoutSubviews method (it calls when autolayout engine end it work, so frames become actual; note that this method can be called multiple times), OR setup autoresizing mask for