iossafearealayoutguide

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

Solution

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