create a view which contains a view that fill the entire view in swift

You can create a view that fills its superview by using Auto Layout and constraints.

  1. Create a new UIView and add it to your existing view as a subview.
main.swift
let myView = UIView()
view.addSubview(myView)
46 chars
3 lines
  1. Set the translatesAutoresizingMaskIntoConstraints property of your view to false to enable Auto Layout.
main.swift
myView.translatesAutoresizingMaskIntoConstraints = false
57 chars
2 lines
  1. Add constraints to your view to pin it to the top, bottom, left, and right of its superview.
main.swift
NSLayoutConstraint.activate([
    myView.topAnchor.constraint(equalTo: view.topAnchor),
    myView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
    myView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
    myView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
288 chars
7 lines

Your view will now fill the entire view of its superview. When you change the size of the superview, the subview will automatically adjust to fill the remaining space.

gistlibby LogSnag