2

In my app I have this error -

safeAreaLayoutGuide' is only available on iOS 11.0 or newer

In this code the error appears 3 times. Basically in each of the rows where I use safeArea.

NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
        ])

Can I just check if IOS 11 is available and run this code and add another code in else statement with the same code but without safeArea. Would that show the view the same as in the if statement. If not are there any other solutions ?

Will this code work on devices that doesn't have IOS 11 the same ? -

        if #available(iOS 11.0, *) {
        NSLayoutConstraint.activate([
            stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
            ])
    } else {
        NSLayoutConstraint.activate([
            stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
            ])
    }
Viktor
  • 75
  • 4
  • 13
  • 1
    See [Safe Area Layout Guide](https://useyourloaf.com/blog/safe-area-layout-guide/). Also had you used **Storyboards**...then it would smartly changes the value between iOS 11 `safeAreaLayoutGuide.top` and iOS<11 `topLayoutGuide.bottomAnchor`. If you don't use storyboards and do it yourself, then you must check the the iOS version and then provide it's appropriate code using `#available`. For a more thorough analysis see [this question](https://stackoverflow.com/q/37796884/5175709) – mfaani Jan 22 '18 at 21:34

2 Answers2

5

safeAreaLayoutGuide is just a replacement for top,bottom layout guides with addition of leading , trailing - of course your'code is good to go and this is the only way to create constraints in code to support IOS 11 and lower versions , but only to switch for constraints that matter not this

stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)

to be this

if #available(iOS 11.0, *) {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor)
     ])
} else {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: self.bottomLayoutGuide.topAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.trailingAnchor)
     ])
      
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Oh okay so I need to add that one line of code - stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50) above if statement for example – Viktor Jan 22 '18 at 21:20
  • yep, no need to switch and look for bottomAnchor change in else – Shehata Gamal Jan 22 '18 at 21:23
  • I got this error now - You're targeting iOS 10.0, but named colors can only be accessed from an Asset Catalog in iOS 11.0 and later. Colors display fine but will they not display if device is less then IOS 11. Can you help me ? – Viktor Jan 22 '18 at 21:55
  • named colors only in iOS 11 + so either you raise your deployment target or surround them with #available and try another alternate prior to 11 – Shehata Gamal Jan 23 '18 at 10:10
0

If you want to be as consistent as possible, in the pre-iOS-11 block, replace view.bottomAnchor with bottomLayoutGuide.topAnchor. Prior to iOS 11, there are still topLayoutGuide and bottomLayoutGuide to help the programmer factor in nav bars, tab bars, etc. Otherwise, looks good to me!

Edit: resulting code:

if #available(iOS 11.0, *) {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
        ])
} else {
    NSLayoutConstraint.activate([
        stackViewBottomConstrols.bottomAnchor.constraint(equalTo: bottomLayoutGuide.topAnchor),
        stackViewBottomConstrols.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        stackViewBottomConstrols.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        ])
}
// This one doesn't care which iOS version it is
stackViewBottomConstrols.heightAnchor.constraint(equalToConstant: 50)
Connor Neville
  • 7,291
  • 4
  • 28
  • 44
  • Look like an accepted answer. So what would be the full code I need to implement look like ? Didn't quite get it. – Viktor Jan 22 '18 at 21:15
  • Edited my answer to add it. – Connor Neville Jan 22 '18 at 21:29
  • I got this error now - You're targeting iOS 10.0, but named colors can only be accessed from an Asset Catalog in iOS 11.0 and later. Colors display fine but wouldn't they display if device is less then IOS 11. Can you help me ? – – Viktor Jan 22 '18 at 22:02