0

I created extension for the view's constraints. You can find the code in the below but after new release iPhone X we need to use safeAreaInsets but i don't know how implement that property. I'll be so glad if you could help me out.

Thanks

    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
        }

    }

}
Krunal
  • 77,632
  • 48
  • 245
  • 261
Esat kemal Ekren
  • 516
  • 2
  • 8
  • 28

1 Answers1

1

I have refactored your code to take into account the insets. Try the following:

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
        }

    }

}
Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
  • I try to implement code that you wrote but it didn't work topInset value always return 0.0 – Esat kemal Ekren Sep 26 '17 at 10:25
  • And are you calling the extension method on the root view of your vc? – Pranav Kasetti Sep 26 '17 at 10:27
  • Yes i inspect step by step also i wrote print function for the topInset value result like Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 Value is : 0.0 – Esat kemal Ekren Sep 26 '17 at 10:27
  • I think i found the problem i use these view in viewdidload method after i changed it viewDidLayoutSubviews it works i think i'll try – Esat kemal Ekren Sep 26 '17 at 10:31
  • This could bring messy for the other elements like if i want to 5 padding top from upper element it will give 5 + topInset.value – Esat kemal Ekren Sep 26 '17 at 10:37
  • Ok I’ve added an extra parameter enableInsets which you can toggle if you don’t want safe area insets. You can see my new edit. – Pranav Kasetti Sep 26 '17 at 10:40