Junior — Middle
Can you explain the differences between UIView and UIWindow in the context of iOS development?
sobes.tech AI
Answer from AI
UIWindow is an object representing the application window. It acts as a container for all UIViews and manages the display of content on the screen. Typically, an iOS application has at least one UIWindow, which is the root of the view hierarchy.
UIView is the base class for all visual interface elements, such as buttons, labels, images, etc. UIView is placed inside a UIWindow or other UIViews and is responsible for rendering and event handling.
Example:
- UIWindow is the "canvas" of the application.
- UIView are the "paintings" and elements drawn on this canvas.
let window = UIWindow(frame: UIScreen.main.bounds)
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
view.backgroundColor = .red
window.addSubview(view)
window.makeKeyAndVisible()