Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get height of topLayoutGuide?

Tags:

ios

swift

 moviePlayer = MPMoviePlayerController(contentURL: url)
 moviePlayer.view.frame = CGRect(x: 0, y:{layoutguide.height}, width:
 self.view.frame.width, height: 300)
 self.view.addSubview(moviePlayer.view)

viewcontroller have an attribute named "topLayoutGuide", but it seems not I wanted.

I know how to implement in storyboard, in code, I can't get the height of Top Layout Guide. I searched for hours getting nothing.

enter image description here

enter image description here

Below is wanted.

enter image description here

like image 691
Albert.Qing Avatar asked May 17 '15 06:05

Albert.Qing


3 Answers

You can get the topLayoutGuide value as its length property:

// Inside your viewController
self.topLayoutGuide.length

Since it is a single value (i.e.: it does not have a height and width) they just called it length. Same holds for bottomLayoutGuide.

Hope this helps

One more thing to mention, in the apple doc for this property:

// As a courtesy when not using auto layout, this value is safe to refer to in -viewDidLayoutSubviews, or in -layoutSubviews after calling super

Use this property in these two functions will get you the accurate value, since the layout has been initialized. If you use it in the viewDidLoad function, this property will be 0.

like image 62
Matteo Piombo Avatar answered Oct 20 '22 03:10

Matteo Piombo


If someone is looking for how to calculate the height of insets of SafeLayoutGuide available for iOS 11 because Top and Bottom Layout Guide are deprecated now, you can find it in:

view.safeAreaInsets

Notice that Top and Bottom Layout Guide were a part of ViewController, and now SafeLayoutGuide is a part of the main view of the ViewController.

like image 29
Vodenjak Avatar answered Oct 20 '22 04:10

Vodenjak


This gets the length (in points) of the portion of a view controller's view that is overlaid by translucent or transparent UIKit bars.

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    let topSpace:CGFloat
    let bottomSpace:CGFloat
    if #available(iOS 11.0, *) {
        topSpace = self.view.safeAreaInsets.top
        bottomSpace = self.view.safeAreaInsets.bottom
    } else {
        topSpace = self.topLayoutGuide.length
        bottomSpace = self.bottomLayoutGuide.length
    }
}
like image 23
RajeshKumar R Avatar answered Oct 20 '22 03:10

RajeshKumar R