Viktor
Viktor

Reputation: 75

How to fix - safeAreaLayoutGuide' is only available on iOS 11.0 or newer

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)
            ])
    }

Upvotes: 2

Views: 4410

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100533

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)
     ])
      
}

Upvotes: 5

Connor Neville
Connor Neville

Reputation: 7361

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)

Upvotes: 0

Related Questions