Fitter Man
Fitter Man

Reputation: 682

OSX Autolayout Fails to Redraw Some Controls As Window Gets Smaller

I've got an xib file for a view that uses autolayout. As the view is made larger, the 3 buttons that are placed along the lower edge of the window all move down as expected, maintaining a constant distance to the lower edge of the window. But as the window is made smaller, the three buttons don't move, eventually disappear and only when the window is made bigger (by even the tiniest amount) do they get redrawn. The animated gif below shows how it looks. UPDATE: This is a newer screen capture. Notice that not only the buttons but also the segmented control does not draw properly as the window resizes.

enter image description here

The constraints are where I'd been focused. For example, the button marked "Check All" has 2 constraints that relate to its vertical position. The other two have to do with leading edge placement and I've not included them here.

A commenter requested that I provide detailed information about the constraints. The first image shows the constraints on the "Check All" button. (The other button has similar constraints.). The constraints are all priority 1000, and they are slightly different from what was in the original post.

enter image description here

This next screenshot shows the constraints on the table above the Check All button.

enter image description here

I have this code on the app delegate:

- (void) windowDidResize: (NSNotification *) notification
{
    _masterViewController.view.frame = ((NSView*)_window.contentView).bounds;
}

If I explicitly add code to this method to redraw the buttons and segmented control that are not drawing properly, everything looks great. That works around the problem, but it still doesn't explain why this happens. UPDATE: It says to me this is probably not an autolayout issue as I originally suspected but something to do with the redraw cycle.

Does anyone have an idea about why this might be behaving as it does?

Upvotes: 0

Views: 82

Answers (2)

Ken Thomases
Ken Thomases

Reputation: 90571

In your code, wherever it is that you do the equivalent of [_window.contentView addSubview:_masterViewController.view], you should do the following:

[_window.contentView addSubview:_masterViewController.view];
_masterViewController.view.translatesAutoresizingMaskIntoConstraints = NO; // optional if you already turned it off in the NIB
NSDictionary* views = @{ @"view": _masterViewController.view };
NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|[view]|"
                                                               options:0
                                                               metrics:nil
                                                                 views:views];
[_window.contentView addConstraints:constraints];
constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|"
                                                      options:0
                                                      metrics:nil
                                                        views:views];
[_window.contentView addConstraints:constraints];

Those constraints will keep _masterViewController.view filling the window's content view.

Upvotes: 1

Kiran Balegar
Kiran Balegar

Reputation: 926

Try adding align Center to Y for those 2(check/uncheck) buttons. Set it to the Transmit button which will also have bottom space to superview.

Basically check/uncheck button should just have height and width constraints plus the align center to Y constraints which is set to the transmit button. Also there should be a constraint between check and uncheck button(leading space one)

Hope this helps.

Upvotes: 0

Related Questions