创建3个视图对象,其中第二个视图是第三个视图的父视图

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 创建3个视图对象,其中第二个视图是第三个视图的父视图 
        let view1 = UIView(frame: CGRect(x: 20, y: 80,  200, height: 200))
        view1.backgroundColor = UIColor.red
        self.view.addSubview(view1)
        
        let view2 = UIView(frame: CGRect(x: 0, y: 0,  200, height: 200))
        view2.bounds = CGRect(x: -40, y: -40,  200, height: 200)
        view2.backgroundColor = UIColor.yellow
        self.view.addSubview(view2)
        
        let viewSub = UIView(frame: CGRect(x: 0, y: 0,  100, height: 100))
        viewSub.backgroundColor = UIColor.blue
        view2.addSubview(viewSub)
    }
}
截屏2020-12-01 下午2.42.51
原文地址:https://www.cnblogs.com/exlo/p/14067966.html