Search code examples
iosswiftviewsize

Get actual view size after setting constraints


How do I get the actual view size? Right now I am getting it from gameView.frame.size. It does give me the right values that I set, but it doesn't give me the actual size.

For example, in my storyboard I have an iPhone 11 and I draw a frame of width:375 and height:375 with constraints.

And now I simulate an iPad and the frame indeed does have the right constraints, but it is still giving me back the values 375-375 but that isn't the actual size.


Solution

  • You have a couple of options; based on your use case:

    1. Get your frame by using:

      DispatchQueue.main.async {
        //print(self.view.frame)
      }
      
    2. get your updated frame values in viewDidAppear

    3. get your updated frame values in viewDidLayoutSubviews. But be careful this is called multiple times.