First make sure your width limits are equal:
A.width == B.width A.width == C.width
And not :
A.width == B.width C.width == B.width
That is, make sure that the representation B is involved in only one constraint of equal width.
Then create output points connected to view B and to an equal width constraint on view B.
@property (nonatomic, strong) IBOutlet UIView *viewB; @property (nonatomic, strong) IBOutlet NSLayoutConstraint *bEqualWidthConstraint;
To connect a limit output, you need to find the limit in the storyboard layout. This will be one of the restrictions for the closest common ancestor A and B; it wonβt be directly on A or B. When you find it in the storyboard path, you can control the drag and drop from it to the socket in the source code (or you can create a new socket by dragging the controls anywhere on your @interface ).
Add another property to set the other B constraint to zero:
@property (nonatomic, strong) NSLayoutConstraint *bZeroWidthConstraint;
Initialize bZeroWidthConstraint , but do not set it:
- (void)viewDidLoad { [super viewDidLoad]; self.bZeroWidthConstraint = [NSLayoutConstraint constraintWithItem:self.viewB attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:0]; }
If you want to hide view B, remove bEqualWidthConstraint and set bZeroWidthConstraint .
In iOS 8.0, you can remove a restriction by setting its active property to NO , but in earlier versions you need to send a message to the view on which the restriction is set. This is usually the closest common ancestor of limited views. Assuming the common ancestor is A and B viewB.superview :
- (void)hideB { [self.viewB.superview removeConstraint:self.bEqualWidthConstraint]; [self.viewB addConstraint:self.bZeroWidthConstraint]; }
Note that since bZeroWidthConstraint restricts viewing to B only, you can set it directly to view B.
If you want to display view B again, remove bZeroWidthConstraint and reinstall bEqualWidthConstraint :
- (void)showB { [self.viewB removeConstraint:self.bZeroWidthConstraint]; [self.viewB.superview addConstraint:self.bEqualWidthConstraint]; }