2017-09-26 6 views
0

보기의 제약 조건에 대한 확장을 생성했습니다. 아래에서 코드를 찾을 수 있지만 새로운 릴리스 이후 iPhone X에서는 safeAreaInsets를 사용해야하지만이 속성을 구현하는 방법을 모르겠습니다. 네가 나를 도울 수 있다면 정말 기뻐할거야.safeAreaInsets를 현재 제약 조건 확장에 통합하는 방법

감사

extension UIView { 

    func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat) { 
     translatesAutoresizingMaskIntoConstraints = false 

     if let top = top { 
      self.topAnchor.constraint(equalTo: top, constant: paddingTop).isActive = true 
     } 
     if let left = left { 
      self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true 
     } 
     if let right = right { 
      rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true 
     } 
     if let bottom = bottom { 
      bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom).isActive = true 
     } 
     if height != 0 { 
      heightAnchor.constraint(equalToConstant: height).isActive = true 
     } 
     if width != 0 { 
      widthAnchor.constraint(equalToConstant: width).isActive = true 
     } 

    } 

} 

답변

1

가 나는 세트 고려해야 할 코드를 리팩토링했다. 다음을 시도해보십시오 :

import UIKit 

extension UIView { 

    func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, enableInsets: Bool) { 
     var topInset = CGFloat(0) 
     var bottomInset = CGFloat(0) 
     var rightInset = CGFloat(0) 
     var leftInset = CGFloat(0) 

     if #available(iOS 11, *), enableInsets { 
      let insets = self.safeAreaInsets 
      topInset = insets.top 
      bottomInset = insets.bottom 
      rightInset = insets.right 
      leftInset = insets.left 
     } 

     translatesAutoresizingMaskIntoConstraints = false 

     if let top = top { 
      self.topAnchor.constraint(equalTo: top, constant: paddingTop+topInset).isActive = true 
     } 
     if let left = left { 
      self.leftAnchor.constraint(equalTo: left, constant: paddingLeft+leftInset).isActive = true 
     } 
     if let right = right { 
      rightAnchor.constraint(equalTo: right, constant: -paddingRight-rightInset).isActive = true 
     } 
     if let bottom = bottom { 
      bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset).isActive = true 
     } 
     if height != 0 { 
      heightAnchor.constraint(equalToConstant: height).isActive = true 
     } 
     if width != 0 { 
      widthAnchor.constraint(equalToConstant: width).isActive = true 
     } 

    } 

} 
+0

나는 당신이 쓴 코드를 구현하려고하지만 당신은 아이폰 OS (11) 폐쇄에 중단 점을 설정 한이 topInset의 값은 항상 0.0 –

+0

를 돌려 작동하지 않았다? –

+0

그리고 vc의 루트보기에서 확장 메서드를 호출하고 있습니까? –