UIView in Swift — Part 2

Janvi Arora
8 min readMar 11, 2023

--

Event-Related Behaviors:

  1. var isUserInteractionEnabled: Bool

A Boolean value that determines whether user events are ignored and removed from the event queue.

When isUserInteractionEnabled is set to true, the view will receive user interactions such as taps, swipes, and other gestures. If set to false, the view will ignore user interactions, and they will be passed to the next view in the responder chain.

During an animation, user interactions are temporarily disabled for all views involved in the animation, regardless of the value in this property. You can disable this behavior by specifying the allowUserInteraction option when configuring the animation.

Default value = true

2. var isMultipleTouchEnabled: Bool

In Swift, the isMultipleTouchEnabled property is a boolean flag on a UIView object that determines whether the view can handle multiple simultaneous touch events.

When isMultipleTouchEnabled is set to true, the view can handle multiple touch events simultaneously, such as pinch-to-zoom gestures or multiple taps. If set to false, the view can only handle one touch event at a time.

It’s important to note that the isMultipleTouchEnabled property does not control whether a view can receive touch events at all - that is controlled by the isUserInteractionEnabled property. If isUserInteractionEnabled is set to false, touch events will not be delivered to the view, regardless of the value of isMultipleTouchEnabled.

NOTE: This property does not affect the gesture recognizers attached to the view. Gesture recognizers receive all touches that occur in the view.

If you want this view to handle multi-touch events exclusively, set the values of both this property and the isExclusiveTouch property to true. This property does not prevent a view from being asked to handle multiple touches.

Default value = false

3. var isExclusiveTouch: Bool

In Swift, the isExclusiveTouch property is a boolean flag on a UIView object that determines whether the view should receive exclusive touch events.

When isExclusiveTouch is set to true, the view will receive all touch events in a sequence of touches, even if another finger touches a different view while the first touch is still in progress. This is useful for scenarios such as buttons or sliders, where you want to ensure that only one touch event is handled at a time.

If isExclusiveTouch is set to false, the view may not receive all touch events in a sequence of touches, if another finger touches a different view in the sequence. This is the default behavior for most views, and is appropriate for scenarios such as scrolling, where you may want to handle multiple touch events simultaneously.

It’s important to note that the isExclusiveTouch property only affects touch events that occur within the view itself - it does not affect how touch events are handled by other views in the responder chain.

In summary, isExclusiveTouch is a property that determines whether a UIView should receive exclusive touch events. It is useful for scenarios where you want to ensure that only one touch event is handled at a time, such as buttons or sliders. The default value of isExclusiveTouch is false.

Default value = false

View Hierarchy Management Variables and Methods:

  1. var superview: UIView?

In Swift, the superview property is a read-only property of a UIView object that represents the view's containing or parent view.

When a UIView object is added to another view using the addSubview(_:) method, the superview property is set automatically to point to the containing view. If the view is not currently part of a view hierarchy (i.e., it has not been added as a subview of any other view), then the superview property will be nil.

2. var subviews: [UIView]

In Swift, the subviews property of a UIView object is an array of UIView objects that represent the view's immediate subviews.

When a UIView object is added as a subview to another view using the addSubview(_:) method, it is appended to the end of the subviews array of the containing view. When a view is removed from its superview using the removeFromSuperview() method, it is also removed from the subviews array of the superview.

The order of the views in the subviews array reflects the order in which they were added to the superview. This means that the view at index 0 is the first view added, and the view at the end of the array is the most recently added.

The subviews property is useful in a variety of scenarios, such as when you need to traverse the view hierarchy, modify the order of the subviews, or perform batch updates on the subviews.

3. var window: UIWindow?

window property of a UIView object represents the top-level UIWindow object that contains the view.

When a UIView object is added to a UIWindow object using the addSubview(_:) method, its window property is automatically set to the containing window. When a view is removed from its superview using the removeFromSuperview() method, its window property is set to nil.

The window property is useful in a variety of scenarios, such as when you need to access the top-level window that contains a specific view, or when you need to perform actions that depend on the window, such as resizing or rotating the view.

4. func addSubview(_ view: UIView)

When you call addSubview(_:) with a UIView object as an argument, it adds that view to the end of the view hierarchy of the receiving view. The added view becomes a subview of the receiving view, and is positioned above all of its other subviews.

let subview = UIView()
parentView.addSubview(subview)

This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

5. func bringSubviewToFront(_ view: UIView)

This method moves the specified view to the end of the array of views in the subviews property, i.e. bring a subview to the front of the view’s hierarchy.

let subview = parentView.subviews.first { $0 is MySubview }
parentView.bringSubviewToFront(subview)

It’s worth noting that if the view you’re moving is already the frontmost subview, calling bringSubviewToFront(_:) will have no effect.

6. func sendSubviewToBack(_ view: UIView)

This method moves the specified view to the front of the array of views in the subviews property, i.e. bring a subview to the end of the view’s hierarchy.

let subview = parentView.subviews.first { $0 is MySubview }
parentView.sendSubviewToBack(subview)

It’s worth noting that if the view you’re moving is already the lastmost subview, calling sendSubviewToBack(_:) will have no effect.

7. func removeFromSuperview()

Used to remove the view from its superview, provided the view’s superview is not nil, the superview releases the view.

When you call removeFromSuperview() on a UIView object, it removes that view from its superview's subviews list and from the view hierarchy altogether. After calling this method, the view is no longer displayed on the screen.

It’s worth noting that if the view you’re trying to remove is not a subview of another view, calling removeFromSuperview() will have no effect.

NOTE From Apple: Never call this method from inside your view’s draw(_:) method.

The reason why you should never call removeFromSuperview() from inside your view's draw(_:) method is because the draw(_:) method is called during the rendering of the view, and is not intended to modify the view hierarchy.

Calling removeFromSuperview() from inside draw(_:) could lead to unexpected behavior or crashes, because the view hierarchy is in the process of being drawn and modified at the same time, which can cause conflicts and inconsistencies.

Instead, you should call removeFromSuperview() from outside of the draw(_:) method, at a point where it is safe to modify the view hierarchy. For example, you could call it from a button tap event handler or in response to a user action that triggers the removal of the view.

8. insertSubview(_:at:)

Inserts a subview at the specified index.

func insertSubview(
_ view: UIView,
at index: Int
)

This method establishes a strong reference to view and sets its next responder to the receiver, which is its new superview.

Views can have only one superview. If view already has a superview and that view is not the receiver, this method removes the previous superview before making the receiver its new superview.

For example, if you have a view hierarchy with three views (view1, view2, and view3), and you want to insert a new view (newView) between view1 and view2, you can call insertSubview(_:at:) as follows:

self.insertSubview(newView, at: 1)

In this example, newView is inserted at index 1, which means it will be placed between view1 (index 0) and view2 (index 2).

9. insertSubview(_:aboveSubview:)

Inserts a view above another view in the view hierarchy.

func insertSubview(
_ view: UIView,
aboveSubview siblingSubview: UIView
)

10. insertSubview(_:belowSubview:)

Inserts a view below another view in the view hierarchy.

func insertSubview(
_ view: UIView,
belowSubview siblingSubview: UIView
)

11. exchangeSubview(at:withSubviewAt:)

Exchanges the subviews at the specified indices.

func exchangeSubview(
at index1: Int,
withSubviewAt index2: Int
)

Each index represents the position of the corresponding view in the array in the subviews property. Subview indices start at 0 and cannot be greater than the number of subviews. This method does not change the superview of either view but simply swaps their positions in the subviews array.

12. func isDescendant(of view: UIView) -> Bool

Used to determine whether the receiver is a descendant of a specified view.

true if the receiver is an immediate or distant subview of view or if view is the receiver itself; otherwise false.

Identifying view at runtime:

  1. var tag: Int

An integer that you can use to identify view objects in your application. You can set the value of this tag and use that value to identify the view later.

The tag property is often used to identify views in a view hierarchy, especially when using the view's superview property to traverse the hierarchy.

Default value = 0

2. func viewWithTag(_ tag: Int) -> UIView?

Returns the view whose tag matches the specified value.

tag here is, the tag value to search for.

It’s important to note that the viewWithTag(_:) method only searches the immediate subviews of the current view. If you want to search the entire view hierarchy, you should call this method on the root view of the hierarchy.

Observing view-related changes:

The default implementation of these methods does nothing. Subclasses can override these methods to perform additional actions whenever the superview changes.

  1. didAddSubview(_:)

Tells the view that a subview was added.

This method is called in response to adding a subview using any of the relevant view methods.

2. willRemoveSubview(_:)

Tells the view that a subview is about to be removed.

3. func willMove(toSuperview newSuperview: UIView?)

Tells the view that its superview is about to change to the specified superview.

4. func didMoveToSuperview()

Tells the view that its superview changed.

5. func willMove(toWindow newWindow: UIWindow?)

Tells the view that its window object is about to change.

newWindow parameter: The window object that will be at the root of the receiver’s new view hierarchy. This parameter may be nil.

6. func didMoveToWindow()

Tells the view that its window object changed.

The window property may be nil by the time that this method is called, indicating that the receiver does not currently reside in any window. This occurs when the receiver has just been removed from its superview or when the receiver has just been added to a superview that is not attached to a window. Overrides of this method may choose to ignore such cases if they are not of interest.

--

--

No responses yet