I'm trying to use preservesSuperviewLayoutMargins through a hierarchy of nested views, but I encounter a lot of problems with UIKit and wonder what I'm doing wrong (or is this a real mistake).
I am trying to lay out a few views (some next to each other, some above each other):
With some layout, I can make it work as shown below. You can see that the images on the right correctly retreat from the green view, which, in turn, correctly retreats from the white view.

But with others I struggle:

Running code like this in the application, I can get a full lock and memory.
Indentation is done using layoutMargins as follows:
let masterView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) masterView.translatesAutoresizingMaskIntoConstraints = false masterView.layoutMargins = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0) let subView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) subView.translatesAutoresizingMaskIntoConstraints = false subView.layoutMargins = UIEdgeInsets(top: 5.0, left: 5.0, bottom: 5.0, right: 5.0)
Here is a sample layout image from the playground. The full project of the playground can be found here , and it can be easily downloaded and run in Xcode to understand what I mean.
class ImageView: UIView { init() { super.init(frame: CGRect(x: 0, y: 0, width: 1000, height: 1000)) self.preservesSuperviewLayoutMargins = true self.layoutMargins = .zero let imageView = UIImageView(image: UIImage(named: "jesus")) imageView.contentMode = .center imageView.translatesAutoresizingMaskIntoConstraints = false imageView.clipsToBounds = true self.clipsToBounds = true imageView.backgroundColor = UIColor.red self.addSubview(imageView) imageView.leftAnchor.constraint(equalTo: self.layoutMarginsGuide.leftAnchor).isActive = true imageView.rightAnchor.constraint(equalTo: self.layoutMarginsGuide.rightAnchor).isActive = true imageView.topAnchor.constraint(equalTo: self.layoutMarginsGuide.topAnchor).isActive = true imageView.bottomAnchor.constraint(equalTo: self.layoutMarginsGuide.bottomAnchor).isActive = true } }