iOS10 UI Tutorial Child View and Parent view UI hierarchy and views inheritance
iOS10 UI Tutorial Child View and Parent view UI hierarchy and views inheritance, This section covers content related to UI hierarchies and views inheritance, including child and parent view, management hierarchy, visibility of views and child views, event hierarchies, and more.
Child View and Parent view
Each instance of a UIView (or subclass) can be connected in a way that other views use a parent-child relationship. Where the parent view is called Superview (Hyper View), the child view is called subviews (Child view). A view can have only one parent view, but there can be multiple child views, as shown in 1.12.
Figure 1.12 Parent and child views
For the parent view, access developers can use the Superview property, which has the following syntax:
var superview:uiview? {Get}
Access to child views can use the Subviews property, which has the following syntax:
var subviews: [UIView] {get}
Example 1-6:superviewandsubview the following will set the background color of the blank view through the Superview property and the Subviews property. The following steps are described:
(1) Open the Main.storyboard file, drag view blank view from the view library to the view controller's main master, and adjust the position and size of this blank view to (16, 107, 343, 423). Declare and associate the socket variable View1 for this view.
(2) Drag from the View library to the second view blank view into the View controller's main view, and place the view in the View1 views, adjusting the position and size of the view to (51, 147, 240, 128). Declare and associate the socket variable view2 for this view.
(3) Open the Viewcontroller.swift file, write the code, and change the background color for the view. The code is as follows:
Import Uikitclass Viewcontroller:uiviewcontroller { @IBOutlet weak var view1:uiview! @IBOutlet weak var view2:uiview! Override Func Viewdidload () { super.viewdidload () //Do any additional setup after loading the view, typically fro M a nib. Let Parentview=view2.superview //Get View2 's parent view let children=view1.subviews //Get child view of View1 Parentview?. backgroundcolor=uicolor.red children[0].backgroundcolor=uicolor.yellow } ...}
When you run the program, you see the effect shown in 1.13.
Figure 1.13 Run effect Figure 1.14 Index
Related reading: iOS10 The central location of the UI tutorial view
iOS10 UI Tutorial Child View and Parent view UI hierarchy and views inheritance