• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Flush two NSViews with one fixed width using Cocoa Auto Layout
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Flush two NSViews with one fixed width using Cocoa Auto Layout


  • Subject: Flush two NSViews with one fixed width using Cocoa Auto Layout
  • From: Tae Won Ha <email@hidden>
  • Date: Sat, 03 May 2014 10:59:42 +0200

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


  • Follow-Ups:
    • Re: Flush two NSViews with one fixed width using Cocoa Auto Layout
      • From: Roland King <email@hidden>
  • Prev by Date: Re: Question on NSScrollView
  • Next by Date: Re: Flush two NSViews with one fixed width using Cocoa Auto Layout
  • Previous by thread: Re: Question on NSScrollView
  • Next by thread: Re: Flush two NSViews with one fixed width using Cocoa Auto Layout
  • Index(es):
    • Date
    • Thread