Re: Flush two NSViews with one fixed width using Cocoa Auto Layout
Re: Flush two NSViews with one fixed width using Cocoa Auto Layout
- Subject: Re: Flush two NSViews with one fixed width using Cocoa Auto Layout
- From: Roland King <email@hidden>
- Date: Sat, 03 May 2014 17:21:21 +0800
Because your drawing code is broken.
NSRectFill( self.frame ) fills starting at the *frame* offset which is 100, so you don't draw the left 100 px. You always want to draw relative to 0,0 in a drawRect: call. Or you could just have set the background colour.
changing it to NSRectFill( dirtyRect ) will do what you want.
There's a menu option, Debug->View Debugging->Show Frame Rects which might have helped you here.
On 3 May, 2014, at 4:59 pm, Tae Won Ha <email@hidden> wrote:
> Hi, guys.
>
> I have a problem using Auto Layout in Cocoa. What I want achieve is
> simple: I have two custom views, say, Left and Right. I want Left to be
> always 100 wide and Right to be flexible, ie resize with the Window:
> +----------+---------------------------+
> | | |
> | Left | Right |
> | 100 wide | flexible |
> | | |
> +----------+---------------------------+
>
> I tried H:|[left(100)][right(>=100)]| as follows:
>
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
> NSView *contentView = self.window.contentView;
>
> DummyView *left = [[DummyView alloc] initWithFrame:CGRectZero];
> left.translatesAutoresizingMaskIntoConstraints = NO;
> left.backgroundColor = [NSColor yellowColor];
> [contentView addSubview:left];
>
> DummyView *right = [[DummyView alloc] initWithFrame:CGRectZero];
> right.translatesAutoresizingMaskIntoConstraints = NO;
> right.backgroundColor = [NSColor greenColor];
> [contentView addSubview:right];
>
> NSDictionary *views = @{
> @"left" : left,
> @"right" : right,
> };
>
> // what am I doing wrong?
> [contentView addConstraints:[NSLayoutConstraint
> constraintsWithVisualFormat:@"H:|[left(100)][right(>=100)]|" options:0
> metrics:nil views:views]];
> [contentView addConstraints:[NSLayoutConstraint
> constraintsWithVisualFormat:@"V:|[left]|" options:0 metrics:nil
> views:views]];
> [contentView addConstraints:[NSLayoutConstraint
> constraintsWithVisualFormat:@"V:|[right]|" options:0 metrics:nil
> views:views]];
> }
>
> The DummyView:
>
> @interface DummyView : NSView
> @property (copy) NSColor *backgroundColor;
> @end
>
> @implementation DummyView
> - (void)drawRect:(NSRect)dirtyRect {
> [_backgroundColor set];
> NSRectFill(self.frame);
> }
> @end
>
> The resulting window looks as follows:
> http://taewon.de/left-right-screenshot.png or
> +----------+----------+---------------------+
> | | | |
> | Left | empty | Right |
> | 100 wide | space | flexible |
> | | | |
> +----------+----------+---------------------+
>
> Why is there an empty space (100 wide) between Left and Right? I also
> tried to set the width constraint of Left using
> +constraintsWithVisualFormat:options:metrics:views:, but I get the same
> result. Am I missing something here?
>
> The project can be also downloaded: https://github.com/qvacua/sandbox
>
> Thanks in advance and best,
> Tae
>
> --
> @hataewon
> http://taewon.de
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden