iOS7 - UIViewController의 topLayoutGuide, bottomLayoutGudie property

어떤 content(status bar, navigation bar, toolbar, tab bar, etc) 에도 덮이지 않는 뷰를 그리기 위해서 제공

iOS 11에서 해당 property deprecated => Safe Area 소개함. (iPhoneX 대응으로 인해 left, right 정보도 필요해짐)

 

UIView

In iOS 11 the UIViewController topLayoutGuide and the bottomLayoutGuide properties have been replaced by the new safe area properties in UIView: => safeAreaInserts, safeAreaLayoutGuide

@available(iOS 11.0, *)
open var safeAreaInsets: UIEdgeInsets { get }
 // UIEdgeInserts - top, left, bottm, right : edge 크기

@available(iOS 11.0, *)
open var safeAreaLayoutGuide: UILayoutGuide { get }
// UILayoutGuide: A rectangular area that can interact with Auto Layout.
// bottomAnchor, heightAnchor, centerXAnchor, ...

방법 1: 뷰의 frame을 safe area에 맞추는 방법

 

topSubview.frame.origin.x = view.safeAreaInsets.left
topSubview.frame.origin.y = view.safeAreaInsets.top
topSubview.frame.size.width = view.bounds.width - view.safeAreaInsets.left - view.safeAreaInsets.right
topSubview.frame.size.height = 300
// or
bottomSubview.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor).isActive = true
bottomSubview.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor).isActive = true
bottomSubview.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
bottomSubview.heightAnchor.constraint(equalToConstant: 300).isActive = true

방법 2 - View안의 label frame을 맞추는 방법

label.frame = safeAreaLayoutGuide.layoutFrame
// or
label.topAnchor.constraint(equalTo: safeAreaLayoutGuide.topAnchor).isActive = true
label.bottomAnchor.constraint(equalTo: safeAreaLayoutGuide.bottomAnchor).isActive = true
label.leftAnchor.constraint(equalTo: safeAreaLayoutGuide.leftAnchor).isActive = true
label.rightAnchor.constraint(equalTo: safeAreaLayoutGuide.rightAnchor).isActive = true

 

 

 

https://medium.com/rosberryapps/ios-safe-area-ca10e919526f

https://devmjun.github.io/archive/SafeArea_1

'iOS > iOS 기본기' 카테고리의 다른 글

토큰 기반의 인증  (0) 2019.12.21
[iOS] Delegate, Notification, KVO 비교 및 장단점 정리  (0) 2019.12.10
[iOS] frame vs. bounds  (0) 2019.12.09
[iOS] int vs NSInteger vs NSNumber  (0) 2019.12.05
[iOS] NSOperation vs. GCD  (0) 2019.11.29

+ Recent posts