Swift Safe Area Layout Guide and Visual Format Language

Jon Vogel picture Jon Vogel · Sep 29, 2017 · Viewed 10.1k times · Source

I want to use Apples visual format language to constrain a view to the new Safe Area Layout Guide in iOS 11. However, I get an exception:

-[NSLayoutYAxisAnchor nsli_superitem]: unrecognized selector sent to instance 0x1c447ed40

    //Make View Dictionary
    var views: [String: Any] = ["left": self.leftContainer]

    //Check swift version and add appropriate piece to the view dictionary
    if #available(iOS 11, *) {
        views["topGuide"] = self.view.safeAreaLayoutGuide.topAnchor
    }else{
        views["topGuide"] = self.topLayoutGuide
    }

    //Make the constraint using visual format language
    let leftVertical = NSLayoutConstraint.constraints(withVisualFormat: "V:[topGuide][left]|", options: [], metrics: nil, views: views)

    //Add the new constraint
    self.view.addConstraints(vertical)

The reason I like visual format language is because you a can add lot of constraints with less code in some cases.

Any Ideas?

Answer

matt picture matt · Sep 29, 2017

I want to use Apples visual format language to constrain a view to the new Safe Area Layout Guide

You can't. There is no access to the safe area layout guide through the visual format language. I've filed a bug on this, and I suggest you do the same.