UIView can be thought of as a rectangular object on the screen

The paper

UIView is the primary way in which an application interacts with the user, and it does the following:

  • Drawing and animation
  • Layout and subView management
  • Event handing

Some basic use of UIView

1. Create and configure the View <View> object, either way adding it to the superview or window
  • Use Interface Builder (IB)

Swift and.xib< empty > files with the same name. Xib custom class specifies swift file 3 with the same name. Import the view from xib in.swift

let className = type(of: self) let bundle = Bundle(for: className) let name = NSStringFromClass(className).components(separatedBy: ".").last let nib = UINib(nibName: name! , bundle: bundle) let view = nib.instantiate(withOwner: self, options: nil).first as! UIViewCopy the code
  • Create using code
let rect = CGRect(x: 10, y: 10, width: 100, height: 100)
let myView = UIView(frame: rect)
Copy the code

The addSubview() method adds a new child view to the parent view. The addSubview() method places the view on top of the parent view. In addition to addSubView(), we can use insertSubview(_:aboveSubview:) and insertSubview(_:belowSubview:) to add a child view that is specified in the Z-axis (X-Y-Z three-dimensional coordinate system) Z axis vertical to the phone screen) order; You can also exchange the location of added subviews using the exchangeSubview(at:withSubviewAt:) method

The drawing cycle of a view

When the view is first displayed, or when part or all of the view is visible due to layout changes. The system is going to ask this view to draw its contents; Views that use UIKit or Core Graphics to contain custom content call the draw(_:) method of the view; When the view content changes, you need to notify the system that the view needs to be redrawn; You can let the system know that it should update the view within the next drawing cycle by calling the view’s setNeedsDisplay() or setNeedsDisplay(_ 🙂 methods. Because the system updates views on the next drawing cycle, these methods can be called on multiple views to update them simultaneously.

Override method

If you inherit UIView, only a few methods have to be overridden, and most methods have to be overridden as needed; UIView is a highly customized class, so you can implement complex views without overwriting parent methods; Here are some methods you can override for UIView subclasses, depending on your needs.

  • Initialization:

    • init(frame:) - This method is recommended, you can also implement the custom initial method;
    • init(coder:) - If you load the view from a StoryBoards or NIB file, you need to customize the initialization method
    • layerClass -
  • Drawing and printing:

    • draw(_:) Implement this method if the View draws custom content. Avoid implementing this method if the view does not have any drawing;
    • draw(_:for:)
  • Layout and Constraints:

    • requiresConstraintBasedLayout
    • updateConstraints()
    • alignmentRect(forFrame:), frame(forAlignmentRect:) -
    • didAddSubview(_:), willRemoveSubview(_:) -
    • willMove(toSuperview:), didMoveToSuperview() -
  • Event Handling:

    • gestureRecognizerShouldBegin(_:) -
    • touchesBegan(_:with:), touchesMoved(_:with:), touchesEnded(_:with:), touchesCancelled(_:with:) -
Create and manage view hierarchies

The view hierarchy is a key part, which not only affects the appearance of the App, but also affects the way in which the App responds to changes and responses. Eg: The parent-child relationship in a view may affect which object handles touch events. The view hierarchy of an interface is as follows:

Adjust the view’s Size and Position at run time

UIView related properties and methods

1. Creating a View

init(frame: CGRect) init? (coder: NSCoder)Copy the code

Configuring a View’s Visual Appearance

// view backgroundColor var backgroundColor: UIColor? Var isHidden: Bool // View transparent var alpha: CGFloat // View transparent var isOpaque: Bool tintColor: UIColor! var tintAdjustmentMode: UIView.TintAdjustmentMode var clipsToBounds: Bool var clearsContextBeforeDrawing: Bool var mask: UIView? class var layerClass: AnyClass var layer: CALayerCopy the code