1

I have couple questions about positioning and autoresizing views with code (without using visual Autolayout constrains tools) Positing and autoresizing views

Question 1:

How to positioning center (X and Y) View 2 in View 1?

Question 2:

How to autoresize View 2 in View 1 and add View 3 (make it center align on View 2)?


P.S.: Sorry for my English

Community
  • 1
  • 1
Lion Gurman
  • 161
  • 1
  • 2
  • 9

3 Answers3

0

For question #1:

You can calculate the x and y values from the superview's width and height. The subview will then be positioned like this:

subView.center = CGPoint(x: superView.frame.size.width/2, y: superView.frame.size.height/2)
LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174
0

Use viewDidLayoutSubviews method to position the views

override func viewDidLayoutSubviews() {
   super.viewDidLayoutSubviews()

   view1.center = self.view.center
   view2.center = self.view1.center
   view3.center = self.view2.center
}
Mohamed Jaleel Nazir
  • 5,776
  • 3
  • 34
  • 48
0

I think you can find your answer in this answer:

https://stackoverflow.com/a/26181982/5160019

Happy coding!

Community
  • 1
  • 1
coskukoz
  • 332
  • 2
  • 20