1. 程式人生 > >iOS之frame和bounds的基本使用

iOS之frame和bounds的基本使用

override func viewDidLoad() {
        super.viewDidLoad()

        let view1 = UIView(frame: CGRect(x: 20, y: 80, width: 280, height: 280))
        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: -20, 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)
}