I want a subclass NSView that actually wraps around an NSSplitView containing a few other views. The problem is, the split view is ignoring any directions to set the position of its dividers (marked in red):
I also wrote this category method on NSLayoutConstraint:
I tested this by dragging an NSView into a window in MainMenu.xib and assigning it to my subclass.
I've set a variety of values for the methods I highlighted in red and no matter what they seem to be ignored. I know they're being executed because everything else in that method works.
Code:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
- (void) setup
{
self.verticalSplitView = [NSSplitView new];
self.verticalSplitView.dividerStyle = NSSplitViewDividerStyleThin;
[self.verticalSplitView setVertical:YES];
NSTextView *textView1 = [NSTextView new];
NSTextView *textView2 = [NSTextView new];
NSTextView *textView3 = [NSTextView new];
textView1.backgroundColor = [NSColor redColor];
textView2.backgroundColor = [NSColor blueColor];
textView3.backgroundColor = [NSColor magentaColor];
[self.verticalSplitView addSubview:textView1];
[self.verticalSplitView addSubview:textView2];
[self.verticalSplitView addSubview:textView3];
[self addSubview:self.verticalSplitView];
self.verticalSplitView.translatesAutoresizingMaskIntoConstraints = NO;
[self addConstraints:[NSLayoutConstraint constraintsFillingChild:self.verticalSplitView]];
[color=RED][self.verticalSplitView setPosition:40 ofDividerAtIndex:1];
[self.verticalSplitView setPosition:20 ofDividerAtIndex:0];[/COLOR]
}
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor redColor] setFill];
NSRectFill(dirtyRect);
}
I also wrote this category method on NSLayoutConstraint:
Code:
+ (NSArray *)constraintsFillingChild:(id)view
{
NSDictionary *views = NSDictionaryOfVariableBindings(view);
NSArray *v = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|"
options:0
metrics:nil
views:views];
NSArray *h = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|"
options:0
metrics:nil
views:views];
NSMutableArray *constraints = [[NSMutableArray alloc] initWithCapacity:4];
[constraints addObjectsFromArray:v];
[constraints addObjectsFromArray:h];
return constraints;
}
I tested this by dragging an NSView into a window in MainMenu.xib and assigning it to my subclass.
I've set a variety of values for the methods I highlighted in red and no matter what they seem to be ignored. I know they're being executed because everything else in that method works.