I have the typical MVC paradigm: an NSViewController that controls an NSTableView. If the table view is empt, the controller needs to display an image in the middle of the table view. Obviously, this image needs to stay in the middle of its superview, the table view.
I display it like this:
NSImageView* emptyPlaceholderView = [NSImageView new];
emptyPlaceholderView.image = [NSImage messagesTableViewEmptyConversationImage];
NSSize imageSize = emptyPlaceholderView.image.size;
NSRect viewFrame = self.view.bounds;
NSRect emptyPlaceholderFrame = (NSRect){{NSMidX(viewFrame)-imageSize.width/2.0f, NSMidY(viewFrame)-imageSize.height/2.0f}, imageSize};
emptyPlaceholderView.frame = emptyPlaceholderFrame;
[self.view addSubview:emptyPlaceholderView];
[emptyPlaceholderView addConstraint:[NSLayoutConstraint constraintWithItem:emptyPlaceholderView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:NSHeight(emptyPlaceholderFrame)]];
[emptyPlaceholderView addConstraint:[NSLayoutConstraint constraintWithItem:emptyPlaceholderView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:NSWidth(emptyPlaceholderFrame)]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:emptyPlaceholderView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0f]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:emptyPlaceholderView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0f]];
self.emptyPlaceholderView = emptyPlaceholderView;
This works fine until I resize the window it resides in. It then crashes with the log:
2013-11-29 08:27:04.677 Messenger[924:303] Unable to simultaneously satisfy constraints:
( "NSAutoresizingMaskLayoutConstraint:0x61000028e150 h=--& v=--& H:[BTRView:0x618000153350(985)]", "NSLayoutConstraint:0x610000488390 NSImageView:0x61000017c200.centerX == BTRView:0x618000153350.centerX", "NSLayoutConstraint:0x61000048a0f0 H:[NSImageView:0x61000017c200(114)]", "NSAutoresizingMaskLayoutConstraint:0x618000498e70 h=--& v=--& H:|-(444)-[NSImageView:0x61000017c200] (Names: '|':BTRView:0x618000153350 )" )
The last constraint, which specifies an x offset of 444, looks weird to me. It seems unnecessary and I don't really get how it is added in the first place.
Setting an autoresizing mask instead of the constraints works just fine:
emptyPlaceholderView.autoresizingMask = NSViewMaxXMargin|NSViewMaxYMargin|NSViewMinXMargin|NSViewMinYMargin;
What am I doing wrong? Can someone explain me where the last constraint comes from and how I could solve this issue using auto layout?