3

For a view controller like the Settings one shown in the below image, how to get the height of a view controller's view without any dependency to the AutoLayout?

Is there any system variable or macro we can use like UIScreen.main.bounds.size to get the screen size?

AutoLayout is not good to me since I need to pre-calculate the frames for further display.

demo

anb
  • 265
  • 1
  • 8

2 Answers2

1
  1. First get the current view height by view.frame.height
  2. Then get the main screen height (device screen height) by UIScreen.main.bounds.height
  3. After that Subtract current view height from thr main screen height.

Below is the sample code:-

let currentViewHeight:CGFloat = view.frame.height
let mainScreenHeight:CGFloat = UIScreen.main.bounds.height
let heightYouNeed = mainScreenHeight-currentViewHeight

Here view is the UIView that is being presented.

Saurav_Sharma
  • 130
  • 1
  • 10
1

At which point do you need it to be calculated? I think the previous answers suggesting to subtract UIScreen.main.bounds.height - view.frame.height should be taken with precaution, because in viewDidLoad the actual frame will be different than in viewDidAppear

OldTimes
  • 300
  • 1
  • 3
  • 16