……

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

import UIKit

class ViewController: UIViewController {

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

posted on 2020-12-01 14:46  Exlo  阅读(74)  评论(0编辑  收藏  举报

导航