Created
July 20, 2020 06:52
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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