Snapkit: Enhancement for SafeArea

Created on 26 Feb 2018  路  8Comments  路  Source: SnapKit/SnapKit

New Issue Checklist

馃毇 If this template is not filled out your issue will be closed with no comment. 馃毇

  • [x] I have looked at the Documentation
  • [x] I have read the F.A.Q.
  • [x] I have filled out this issue template.

Issue Info

Info | Value |
-------------------------|-------------------------------------|
Platform | e.g. ios
Platform Version | e.g. 10.0
SnapKit Version | e.g. 4.0.0
Integration Method | e.g. carthage

Issue Description

Hi, I am using SnapKit in my project, Thanks for providing such a grate library.

Since the update of iOS 11, the safeArea was imported, and thanks very much for adding the safeArea extension in SnapKit. Because I need to support the iOS10.0, I have to check the os version in the make closure, I am wondering if it is possible to make the code like

aView.snp.make { (make) in
   //other constraints
  if #available(iOS 11.0 *) {
      make.snp.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
  } else {
      make.snp.bottom.equalTo(view)
  }
}

to

aView.snp.make { (make) in
   //other constraints
   make.snp.safeAreaBottom.equalTo(view)
}

Most helpful comment

@JonyFang Thanks for your answer, But I am thinking a way if the iOS version judgement can be move to the SnapKit.

All 8 comments

@Cookiezby BTW if you are using safeAreaLayoutGuide.snp.top to align elements you are more likely want to use topLayoutGuide.snp.bottom from current UIViewController:

// Inside view controller
sampleView.snp.makeConstraints { make in 
  if #available(iOS 11.0, *) {
    make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).inset(20)
  } else {
    make.top.equalTo(self.topLayoutGuide.snp.bottom).inset(20)
  }
}

I am using safeAreaLayoutGuide in my project. safeAreaLayoutGuide is a UILayoutGuide, which is already relatable in SnapKit via snp. Here is an example:

view.addSubview(tableView)
tableView.snp.makeConstraints { (make) in
    make.left.right.equalTo(view)
    if #available(iOS 11.0, *) {
        make.top.equalTo(view.safeAreaLayoutGuide.snp.topMargin)
    } else {
        make.top.equalTo(view)
    }
    if #available(iOS 11.0, *) {
        make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottomMargin)
    } else {
        make.bottom.equalTo(view)
    }
}

@JonyFang Thanks for your answer, But I am thinking a way if the iOS version judgement can be move to the SnapKit.

Any update for this?

That would be really great! Any update for this?

Any update?

don't know if it helps

//
//  ClearTopBar.swift
//  Instories
//
//  Created by Vladyslav Yakovlev on 22.02.2018.
//  Copyright 漏 2018 Vladyslav Yakovlev. All rights reserved.
//

import UIKit
import SnapKit

extension UIView {
    var safeArea: ConstraintBasicAttributesDSL {
        if #available(iOS 11.0, *) {
            return self.safeAreaLayoutGuide.snp
        }
        return self.snp
    }

    func dropShadow() {
        self.layer.masksToBounds = false
        self.layer.shadowColor = UIColor.lightGray.cgColor
        self.layer.shadowOpacity = 1.0
        self.layer.shadowRadius = 10
        self.layer.shadowOffset = CGSize(width: 3, height: 3)
    }

    var isIphoneX: Bool {

        if #available(iOS 11.0, *) {
            if topSafeAreaInset > CGFloat(0) {
                return true
            } else {
                return false
            }
        } else {
            return false
        }

    }

    var topSafeAreaInset: CGFloat {
        let window = UIApplication.shared.keyWindow
        var topPadding: CGFloat = 0
        if #available(iOS 11.0, *) {
            topPadding = window?.safeAreaInsets.top ?? 0
        }

        return topPadding
    }

    var bottomSafeAreaInset: CGFloat {
        let window = UIApplication.shared.keyWindow
        var bottomPadding: CGFloat = 0
        if #available(iOS 11.0, *) {
            bottomPadding = window?.safeAreaInsets.bottom ?? 0
        }

        return bottomPadding
    }
}

not using the above / instead I'm targeting different devices as below

DISCLAIMER - I own an iphone 8 - so this logic is necessary for my code.

PSUEDOCODE here

  pod 'Device', :git => 'https://github.com/ozgur/Device.git'

    myViewHere.snp.makeConstraints { (make) in
            if Device.needsSafeAreaBecausePhoneIsHuge() {
                make.bottom.equalToSuperview().offset(-100)
            } else {
                make.bottom.equalToSuperview().offset(-40)
            }
            make.width.left.equalToSuperview()
            make.height.equalTo(93)
        }

extension Device {

    static func needsSafeAreaBecausePhoneIsHuge() -> Bool {
        let version = Device.versionOnSimulator()
        switch version {
        case .iPhoneX, .iPhoneXR, .iPhoneXS, .iPhoneXS_Max, .iPhone11, .iPhone11Pro, .iPhone11Pro_Max, .iPhone12, .iPhone12Pro, .iPhone12Pro_Max:
            return true
        default:
            return false
        }
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

phongle6893 picture phongle6893  路  4Comments

alexpersian picture alexpersian  路  4Comments

danieleggert picture danieleggert  路  3Comments

semiwhale picture semiwhale  路  3Comments

seljabali picture seljabali  路  3Comments