'Creating a UIView in a separate class and installing constrains

ViewController:

class ViewController: UIViewController, ViewSpecificController {
typealias RootView = CustomView
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view().configure()
    }

    override func loadView() { self.view = CustomView() }
}

UIView:

class CustomView: UIView {
    func configure() {
        backgroundColor = .orange
        translatesAutoresizingMaskIntoConstraints = false
        addConstraints()
        
    }
    func addConstraints() {
        var constraints = [NSLayoutConstraint]()
        constraints.append(self.leadingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.leadingAnchor))
        constraints.append(self.trailingAnchor.constraint(equalTo: self.safeAreaLayoutGuide.trailingAnchor))
        constraints.append(self.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor))
        constraints.append(self.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor))
        NSLayoutConstraint.activate(constraints)
    }
}

Executing this code results in an error "[LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want." I tried to initialize UIView, the same error appeared there. How to fix it?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source