Skip to content

Instantly share code, notes, and snippets.

@simrandotdev
Created July 20, 2020 06:52
Show Gist options
  • Select an option

  • Save simrandotdev/64fcf49537425dc416ff1e8a3de3e616 to your computer and use it in GitHub Desktop.

Select an option

Save simrandotdev/64fcf49537425dc416ff1e8a3de3e616 to your computer and use it in GitHub Desktop.
Adding views in other views and checking which view is contained in what UIView
func basicsOfViews() {
let v = UIView(frame: CGRect(x: 100, y: 100, width: 150, height: 150))
v.backgroundColor = .red
self.view.addSubview(v)
v.layer.masksToBounds = true
let v2 = UIView(frame: CGRect(x: 130, y: 140, width: 150, height: 150))
v2.backgroundColor = .yellow
self.view.addSubview(v2)
let v3 = UIView(frame: CGRect(x: 10, y: 10, width: 250, height: 250))
v3.backgroundColor = .green
v3.tag = 101
v2.addSubview(v3)
// This won't allow the subviews to leak out of the super view.
v2.clipsToBounds = true
print(v2.isDescendant(of: view))
print(v3.isDescendant(of: v2))
print(v3.isDescendant(of: view))
print(v2.isDescendant(of: view))
print(v.isDescendant(of: view))
// Find the View by its Tag.
let v101 = v3.viewWithTag(101)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment