iOS development - Creating a view programmatically -

iOS development - Creating a view programmatically

ios, programming, mobile development, views, uiview

When creating a new view, its position and size must be specified.

let rect = CGRect(x: 10, y: 10, width: 100, height: 100) // view will be 100x100, with its origin (top-left corner) at coordinate (10, 10) of the superview
let myView = UIView(frame: rect)

To add a subview to a superview, use the superview’s addSubview(_:) method.

To control the z-index of subviews, use the insertSubview(_:aboveSubview:) and insertSubview(_:belowSubview:) methods, as well as the exchangeSubview(at:withSubviewAt:) method to switch the positions of two subviews.

Auto Layout can be used to provide layout logic for views. Some frameworks provide this as well, such as Yoga.