aejhyun
aejhyun

Reputation: 612

Learning about the view hierarchy in iOS

AFTER writing the code mentioned directly below,

//
//  FirstCustomSegue.swift
//  Prayer
//
//  Created by Jae Hyun Kim on 8/9/15.
//  Copyright (c) 2015 Jae Hyun Kim. All rights reserved.
//

import UIKit

class FirstCustomSegue: UIStoryboardSegue {
    override func perform() {
        var firstVCView = self.sourceViewController.view as UIView!
        var secondVCView = self.destinationViewController.view as UIView!
        let screenWidth = UIScreen.mainScreen().bounds.size.width
        let screenHeight = UIScreen.mainScreen().bounds.size.height
        secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight)
    }
}

One of the statements in a tutorial that I am reading says, "At this point, the view of the second view controller is not a subview of the app’s window yet. So, before we implement the actual animation, it’s obvious that we must add it to the window. This will be achieved using the insertSubview(view:aboveSubview:) method of the app’s window object. As you see next, at first we access the window object, and then we add the destination view:" Then he goes to add the following code:

override func perform() {
    ...

    // Access the app's key window and insert the destination view above the current (source) one.
    let window = UIApplication.sharedApplication().keyWindow
    window?.insertSubview(secondVCView, aboveSubview: firstVCView)

}

However, I'm not exactly sure what he means when he states "At this point, the view of the second view controller is not a subview...and blah blah blah" What does this mean? I would be more specific about what my question is about the statement such as what does "subview" mean. But I'm confused about pretty much everything about the statement. I'm sorry :(

And what does this statement, secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight), mean?

Upvotes: 1

Views: 295

Answers (1)

luk2302
luk2302

Reputation: 57114

The view hierarchy is one core element to understand regarding iOS user interface. It basically says that all views on screen are in some kind of relation to each other - subviews, parent views, sibling views.

  • subviews are just views that are nested in another view and are subviews of that view
  • parent view / superviews are the counterpart - the view somethings is nested in is its superview
  • sibling views are views that are contained in the same parent view

Note that views can be all three roles at the same time in respect to different views. If viewA contains viewB and viewC while viewB contains viewD then

  • viewA is the superview of viewB and viewC
  • viewB and viewC are subviews of viewA
  • viewA and viewB are siblings
  • viewB is the superview of viewD
  • viewD is a subview of viewB
  • => viewB fulfills all three roles.

Everything on screen needs to be part of the view hierarchy for the OS to be able to determine its place to be rendered.

A quite important thing about the view hierarchy is the relative positioning of views. The positioning is always relative to ones superview. Between sibling views and their parent view there can be setup layout constraints (-> Autolayout) to manage their layout.

Note that this is a quite brief overview and far from perfect, I highly recommend to read through the apple docs regarding view hierarchy at least once.

Now regarding your questions:

"At this point, the view of the second view controller is not a subview...and blah blah blah" What does this mean?

That means exactly what it says, the view is not part of the explained hierarchy. Therefore the OS does not know yet where to place it - it actually does not even know that it should render it at all. To fix that you have to add the view somewhere in the hierarchy by picking a view and adding your new view as a subview there.

And what does this statement, secondVCView.frame = CGRectMake(0.0, screenHeight, screenWidth, screenHeight), mean?

That line sets the frame property of the frame of secondVCView to be equal a newly created CGRect with the specified dimensions. The frame determines the view's position and size. To understand that property and its counterparts is equally essential to understanding how views and their hierarchy work. Luckily there is this answer on SO that covers that topic quite well.

Upvotes: 2

Related Questions