0% found this document useful (0 votes)
12 views

Summary of UIViewController Lifecycle Call Order

Uploaded by

Pqrswed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Summary of UIViewController Lifecycle Call Order

Uploaded by

Pqrswed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 4

Summary of UIViewController Lifecycle Call Order:

1 init
2 loadView
3 viewDidLoad
4 viewWillAppear:
5 viewWillLayoutSubviews:
6 viewDidLayoutSubviews:
7 viewDidAppear:
8 viewWillDisappear:
9 viewDidDisappear:
10 dealloc

What are the different states of UIView?

The lifecycle of a `UIView` in iOS involves several key states that


determine how the view is initialized, displayed, updated, and
removed. Each state corresponds to specific methods in the
`UIView` class. Here's an overview of the different states of a
`UIView` and their corresponding methods:

### 1. **Initialization**
- **`initWithFrame:`**: Called when a view is initialized
programmatically with a specific frame.
- **`initWithCoder:`**: Called when a view is initialized from a
storyboard or nib file. This is used when the view is loaded from an
archive or interface builder.

### 2. **Loading**
- **`awakeFromNib`**: Called after the view has been loaded from a
nib or storyboard. This is where you can perform additional setup
that couldn't be done during initialization.

### 3. **Adding the View to the View Hierarchy**


- **`willMoveToSuperview:`**: Called right before the view is added to
a superview. This method allows you to prepare the view or make
adjustments before it becomes part of the view hierarchy.
- **`didMoveToSuperview`**: Called right after the view is added to a
superview. This is where you can trigger additional actions now that
the view is part of the hierarchy.
- **`willMoveToWindow:`**: Called before the view is added to a
window. Useful for preparing the view before it appears on the
screen.
- **`didMoveToWindow:`**: Called after the view has been added to a
window. You might use this to start animations or begin observing
notifications.
### 4. **Layout and Rendering**
- **`setNeedsLayout`**: Marks the view as needing layout, which will
trigger a layout update in the next layout pass. This does not
immediately cause a layout, but schedules one.
- **`layoutIfNeeded`**: Forces an immediate layout update. This will
cause the view and its subviews to be laid out if they need it.
- **`layoutSubviews`**: Called whenever the view’s bounds change or
when a layout pass is triggered. This is where you define how
subviews should be arranged.
- **`drawRect:`**: Called when the view is about to be rendered. This
method is used for custom drawing. It’s called automatically when
the view is first drawn or when it’s invalidated by calling
`setNeedsDisplay`.

### 5. **View Updates**


- **`setNeedsDisplay`**: Marks the view as needing to be redrawn. This
will trigger a call to `drawRect:` in the next drawing cycle.
- **`setNeedsDisplayInRect:`**: Marks a specific portion of the view as
needing to be redrawn.

### 6. **Touch Events and Interaction**


- **`touchesBegan:withEvent:`**, **`touchesMoved:withEvent:`**,
**`touchesEnded:withEvent:`**, **`touchesCancelled:withEvent:`**:
These methods are called when the user interacts with the view.
They are part of the UIResponder chain and allow the view to handle
touch events.

### 7. **Removing the View from the View Hierarchy**


- **`removeFromSuperview`**: Called when the view is removed from
its superview. This method can be used to clean up resources or
perform other tasks related to the view's removal.
- **`willMoveToSuperview:`**: Called with a `nil` parameter when the
view is about to be removed from its superview.

### 8. **Deallocation**
- **`dealloc`**: This is called when the view is being deallocated from
memory. It's important to release any retained resources or
observers here.

### Summary of Lifecycle Call Order (Common Cases):


1. `initWithFrame:` or `initWithCoder:`
2. `awakeFromNib`
3. `willMoveToSuperview:`
4. `didMoveToSuperview`
5. `willMoveToWindow:`
6. `didMoveToWindow:`
7. `layoutSubviews`
8. `drawRect:`
9. **Touch Events Handling**
10. `removeFromSuperview`
11. `dealloc`

Understanding these states and corresponding methods allows you to


effectively manage your `UIView`'s lifecycle, optimizing
performance and user experience in your app.

What are the differences between clipsToBounds and masksToBounds?


`clipsToBounds` and `masksToBounds` are both properties used in iOS
development to control how views and their sublayers are rendered, but
they apply to different layers in the view hierarchy and have distinct
purposes. Here's a detailed comparison:

### **1. `clipsToBounds` (UIView property)**


- **Context**: This property belongs to `UIView`.
- **Functionality**: When `clipsToBounds` is set to `true`, any subviews
that extend outside the bounds of the parent view are clipped, meaning
they are not rendered outside the view's bounds.
- **Usage**: This is commonly used when you want to ensure that a view's
subviews do not render outside the intended area. For example, if you
have a view with a custom shape and you want to restrict the content of
its subviews to be visible only within that shape.
- **Example**:
```swift
view.clipsToBounds = true
```
With this setting, if a subview extends beyond the edges of the `view`,
the part of the subview outside the `view`'s bounds will not be visible.

### **2. `masksToBounds` (CALayer property)**


- **Context**: This property belongs to `CALayer`, which is the underlying
layer of a `UIView`.
- **Functionality**: When `masksToBounds` is set to `true`, any sublayers
(not subviews) that extend outside the bounds of the layer are clipped.
Additionally, this property also affects layer properties such as
`cornerRadius`, `shadow`, and `border`. For example, if you set a
`cornerRadius` on a layer, setting `masksToBounds` to `true` will ensure
that the contents of the layer are clipped to match the rounded corners.
- **Usage**: This is typically used when working with layers directly,
particularly when you want to apply effects like corner rounding, and
ensure that the contents of the layer and its sublayers respect the layer’s
bounds.
- **Example**:
```swift
view.layer.masksToBounds = true
```
With this setting, the content of the layer, including its sublayers, will be
clipped to the layer's bounds. If you set a `cornerRadius`, the layer's
contents will be clipped to the rounded corners.

### **Key Differences**


1. **Level of Application**:
- `clipsToBounds` applies to the `UIView` and affects how the view
clips its subviews.
- `masksToBounds` applies to the `CALayer` and affects how the layer
clips its sublayers and its content.

2. **Effect on Layer Properties**:


- `masksToBounds` is directly tied to the rendering of layer properties
like `cornerRadius`, `shadow`, and `border`. `clipsToBounds` does not
affect these layer properties; it only affects the view's subviews.

3. **Subviews vs. Sublayers**:


- `clipsToBounds` clips subviews (i.e., other `UIView` objects within the
view).
- `masksToBounds` clips sublayers (i.e., other `CALayer` objects within
the layer).

4. **Common Use Cases**:


- Use `clipsToBounds` when you are managing a view hierarchy and
need to ensure that subviews do not render outside the bounds of a
parent view.
- Use `masksToBounds` when dealing with layers directly, particularly
when you want to create visual effects like rounded corners or to clip
sublayers to the bounds of the layer.

In summary, `clipsToBounds` is a higher-level property for views, while


`masksToBounds` is a lower-level property for layers, with more impact
on how layers and their properties are rendered.

You might also like