class ViewController: UIViewController {
var view1:UIView! var view2:UIView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let view1Width = self.view.bounds.width / 2 let view1Hight = self.view.bounds.height / 2 let view1X = self.view.bounds.width / 2 let view1Y = self.view.bounds.height / 2 view1 = UIView(frame: CGRect(x: view1X, y: view1Y, width: view1Width, height: view1Hight)) view1.backgroundColor = .red self.view.addSubview(view1) let view2Width = view1Width / 2 let view2Height = view1Hight / 2 let view2X = self.view.bounds.width / 4 let view2Y = self.view.bounds.height / 4 view2 = UIView(frame: CGRect(x:view2X , y: view2Y, width: view2Width, height: view2Height)) view2.backgroundColor = .blue self.view.addSubview(view2) }
}
class ViewController: UIViewController {
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. let view1 = UIView() view1.frame.size = CGSize(width: self.view.bounds.width / 2, height: self.view.bounds.height / 2) view1.layer.position = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2) view1.backgroundColor = .red self.view.addSubview(view1) let view2 = UIView() view2.frame.size = CGSize(width: self.view.bounds.width / 4, height: self.view.bounds.height / 4) view2.layer.position = CGPoint(x: self.view.bounds.width / 4, y: self.view.bounds.height / 4) view2.backgroundColor = .blue self.view.addSubview(view2) }
}
なぜこれは同じ場所を指定しているのに実行すると配置場所が異なるのはなぜでしょうか?
view2 = UIView(frame: CGRect(x:view2X , y: view2Y, width: view2Width, height: view2Height))イニシャライザで指定するのと
view1.frame.size = CGSize(width: self.view.bounds.width / 2, height: self.view.bounds.height / 2)
view1.layer.position = CGPoint(x: self.view.bounds.width / 2, y: self.view.bounds.height / 2)このような書き方をするのとではなぜ違いが出るのでしょうか?
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。