SoFunction
Updated on 2025-04-11

Swift automatically adjusts view layout AutoLayout and AutoresizingMask functions detailed explanation

AutoLayout and AutoresizingMask in Swift

AutoLayout and AutoresizingMask in Swift are used to automatically adjust the layout of views to suit different sized devices and screen orientations.

Both methods can be used to deal with automatic adaptation of views, but their implementations are different.

AutoLayout

AutoLayout is a constraint-based view layout system that allows views to automatically adapt to different device sizes and orientations according to constraints. By adding constraints, AutoLayout calculates the correct position and size of each view to make sure they are always in the correct position and are the correct size.

Here are some common AutoLayout constraints:

  • Width constraint or height constraint: limits the width or height of the view;
  • Horizontal or vertical spacing constraints: limit spacing between views;
  • Top, bottom, left, right constraints: limit the position of the view in the parent view.

Here is a simple example code that demonstrates how to use itAutoLayoutSet a view to center horizontally and vertically in the parent view:

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        ()
        let myView = UIView()
         = .red
         = false // Close AutoresizingMask and turn on AutoLayout        (myView) // Add myView to parent view        ([
            (equalTo: ), // Centered in the X-axis direction            (equalTo: ), // Centered in the Y-axis direction            (equalToConstant: 200), // Set the height to 200            (equalTo: , multiplier: 0.8) // Width is 0.8 times the height        ])
    }
}

The above code creates a red oneUIViewobject and add it to the current oneViewControlleron the main view. use()The method adds a set of constraints to determine the position and size of the view, wheremyViewThe center point of the parent view coincides with the center point of the parent view, and the height and width of the view are set.

AutoresizingMask

AutoresizingMask is a view layout system based on the autoresizingMask property. It allows the view to automatically resize and position according to the screen orientation. When the view adjusts the width or height, the AutoresizingMask property adjusts the layout of the subview based on the layout information of the view.

Here are some common AutoresizingMask properties:

  • UIViewAutoresizingFlexibleWidth: Automatically adjust width
  • UIViewAutoresizingFlexibleHeight: Automatic height adjustment
  • UIViewAutoresizingFlexibleLeftMargin: Automatically adjust the left margin
  • UIViewAutoresizingFlexibleRightMargin: Automatically adjust the right margin
  • UIViewAutoresizingFlexibleTopMargin: Automatically adjust the top distance
  • UIViewAutoresizingFlexibleBottomMargin: Automatically adjust the bottom distance

Next, let's see how to use itAutoresizingMaskAdd layout constraints to implement adaptive layouts. Here is a sample code that demonstrates how to use itautoresizingMaskProperties center a view and resize it according to the direction of the screen:

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        ()
        let myView = UIView()
         = .red
         = CGRect(x: 0, y: 0, width: 200, height: 200)
         =  // Set the view center         = [.flexibleWidth, .flexibleHeight, .flexibleTopMargin, .flexibleBottomMargin] // Adaptive width, height and top and bottom margins        (myView)
    }
}

The above code creates a red UIView object and adds it to the main view of the current ViewController. useautoresizingMaskProperties set the position and size of the view to be adaptively adjusted, where both width and height are adaptive, while the top and bottom margins can be adjusted as the screen orientation changes.

It is worth noting that AutoresizingMask does not support fine constraint settings, and can only simply specify adaptive methods, which are not suitable for situations where more advanced layouts are required.

Summarize

The implementation of AutoLayout is more refined and flexible, and can use constraints to create more different layout effects, but the AutoresizingMask property control is simple, suitable for quickly implementing simple layouts. It is necessary to choose which adaptive layout method to use according to specific needs and design.

The above is the detailed explanation of the AutoLayout and AutoresizingMask functions for Swift. For more information about Swift AutoLayout AutoresizingMask, please follow my other related articles!