Ken
Ken

Reputation: 1461

How to find View Controller height programmatically?

I have a simple question, how do I find the height of the View Controller programmatically when I have it set to freeform?

I want to do something like:

let viewControllerHeight = viewController.contentSize.height

This is what I'm trying to find

Also, how do I post an image on here without it being a link?

Upvotes: 2

Views: 7470

Answers (4)

Andrey Chernukha
Andrey Chernukha

Reputation: 21808

There's no such thing as UIViewController height because this class is only a CONTROLLER of a view. You can get the height of its view in a very simple way

let height = viewController.view.frame.size.height

Please note that this value may be incorrect in viewDidLoad and you need to check it later. Find out more

Upvotes: 5

vacawama
vacawama

Reputation: 154583

The simulated size affects the size of the UIViewController’s view in the Storyboard. If you access self.view.frame.size.height in viewDidLoad() you will see that value. After the view appears, in viewDidAppear(), the frame of the view will be adjusted for the screen size and orientation of the device your app is running on.

Until you have enough rep, your pictures show as links. You can fix this by editing your question or answer and adding a ! before the link usage like I did for your question.

Upvotes: 1

Asad Ali Choudhry
Asad Ali Choudhry

Reputation: 5261

Actually the safe area height is your viewController height. So you can find it like this

let guide = self.view.safeAreaLayoutGuide
let height = guide.layoutFrame.size.height

Please have a look on this Link

Upvotes: 0

Andrew21111
Andrew21111

Reputation: 908

Try this code:

  let viewControllerHeight = viewController.view.frame.size.height

Upvotes: 0

Related Questions