Explore core concepts of UIKit with this quiz focused on views, view controllers, and navigation fundamentals. Check your understanding of user interface structure, hierarchy management, navigation patterns, and UIKit basics essential for building intuitive mobile applications.
Which method should you use to add a subview to an existing UIView in UIKit?
Explanation: addSubview() is the correct method to add a child view to an existing view, making it appear within the parent view's hierarchy. insertSubviewAbove() is used for a more specific case when you want to insert a view above another sibling. removeFromSuperview() is for removing a view from its parent. appendChild() is not a UIKit method; it is unrelated in this context.
What is the primary role of a ViewController in the UIKit architecture?
Explanation: ViewControllers are designed to manage the life cycle, layout, and user interactions for a single screen or content area. Drawing graphics directly is usually handled by views or layers, not controllers. While ViewControllers may respond to touch, they do not handle all low-level touch events exclusively. Persistent data storage is handled by other entities like databases or models.
When using a navigation controller, which method should you call to transition to a new ViewController?
Explanation: pushViewController(_:animated:) is the standard method for adding a ViewController to the navigation stack, resulting in a forward navigation transition. presentModalViewController() is deprecated and not specific to navigation stacks. addSubview() is for views, not controllers. showSegue() is not a UIKit method; segues are used in storyboards, but not with this direct method.
Which method is called after a ViewController's view is loaded into memory but before it appears on the screen?
Explanation: viewDidLoad is called once when the view is loaded, and it is ideal for initial setup. viewDidAppear occurs after the view is already visible. viewWillDisappear is called when the view is about to go away. viewWillLayoutSubviews is about to layout its subviews and can be called multiple times, not specifically after loading.
Which property of UIView determines whether or not a view can be seen by the user?
Explanation: isHidden is a Boolean property that controls the visibility of a view. Setting it to true will hide the view from the user. canFocus is not used to control visibility, but is related to focus-based interaction. isSelected is for selectable elements but does not affect basic visibility. isActive is not a standard visibility property in views.
What is the effect of setting a view’s autoresizingMask property in UIKit?
Explanation: autoresizingMask specifies how a view’s size and position adjusts when its superview changes size. It does not hide the view or force redraws. Disabling user interaction requires changing the userInteractionEnabled property. Using autoresizingMask allows for responsive layouts without extra code.
Which method should you use to present one ViewController modally over another in UIKit?
Explanation: present(_:animated:completion:) is used to present a ViewController modally, temporarily covering the existing interface. pushViewController(_:animated:) is for navigation stack transitions, not modal. displayModal() and showViewController() are not valid UIKit methods for presenting a controller.
In a ViewController, what is the main top-level container for all other views?
Explanation: The view property of a ViewController is the main container that holds all other subviews within that controller. superview refers to the parent of a specific view, not the controller’s main container. window represents the topmost UI element in the hierarchy but is managed separately. containerView is a custom concept and not the default property for all controllers.
Why should you use the safeAreaLayoutGuide when placing UI elements in a view?
Explanation: safeAreaLayoutGuide helps developers ensure that content does not overlap system bars or notches, providing a visually consistent layout. It does not animate transitions, alter brightness, or add shadows. Using safe areas is important for proper appearance across different devices.
What happens when you call popViewController(animated:) on a navigation controller?
Explanation: popViewController(animated:) removes the currently visible ViewController from the navigation stack, displaying the one below it. It does not remove all controllers, add new ones, or dismiss the navigation controller itself. This method is used to navigate backwards in stack-based interfaces.