I have a UIView that contains multiple UIView subviews that has its own constraints. How do I remove the subviews' constraints?
//only removes the constraints on self.view
[self.view removeConstraints:self.view.constraints];
//I get warning: Incompatible pointer types sending 'NSArray *' to parameter of type 'NSLayoutConstraint'
[self.subview1 removeConstraints:self.subview1.constraints];
Try this code:
for (NSLayoutConstraint *constraint in self.view.constraints) {
if (constraint.firstItem == self.subview1 || constraint.secondItem == self.subview1) {
[self.view removeConstraint:constraint];
}
}
Basically, this iterates all of the constraints that are assigned to self.view and checks to see whether self.subview1 is involved in the constraint. If so, that constraint gets pulled.
You have to remove all the constraints of the view and its subview. So create an extension of UIView and then define the following method:
extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}
then call the following:
self.view.removeAllConstraints()
As answering later, This is in swift. This may help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With