Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
CAConstraintLayoutManager problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CAConstraintLayoutManager problem



Hi,

I'm having trouble laying out sub-layers with CAConstraintLayoutManager. I expect to get 5 boxes horizontally packed into the super layer, which in IB is set to fill the entire view. I expect the boxes to be equally sized, because I constrain the leftmost and the rightmost to the leftmost and rightmost edges of the superview, and the leftmost edge of the inner boxes to the rightmost edge of the previous. Instead what I get is just a pretty blue background with a single square on it. The only good thing to be said for it is that it is scaled, and positioned, correctly in the vertical dimension.

What am I doing wrong in the below code?


@implementation MyView

- (void)awakeFromNib {
   CALayer *layer = [CALayer layer];
   self.layer = layer;
   self.wantsLayer = YES;

   layer.backgroundColor = CGColorCreateGenericRGB(0, 0, 0.4, 1);
   layer.layoutManager = [CAConstraintLayoutManager layoutManager];

   CALayer *bar;
   NSString *previous = nil;
   NSUInteger count = 5;
   for (int i = 0; i < count; i++) {
       bar = [CALayer layer];
       bar.name = [NSString stringWithFormat:@"bar%d", i];
       bar.borderColor = CGColorCreateGenericGray(1, 0.5);
       bar.borderWidth = 8;
       bar.cornerRadius = 16;

[bar addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintHeight
relativeTo:@"superlayer"
attribute:kCAConstraintHeight
scale:i / (float)count
offset:0]];


[bar addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMidY
relativeTo:@"superlayer"
attribute:kCAConstraintMidY]];


if (!previous)
[bar addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinX
relativeTo:@"superlayer"
attribute:kCAConstraintMinX]];
else
[bar addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMinX
relativeTo:previous
attribute:kCAConstraintMaxX]];


NSLog(@"%@ => %@", previous, bar.name);
previous = bar.name;
[layer addSublayer:bar];
}
[bar addConstraint:[CAConstraint constraintWithAttribute:kCAConstraintMaxX
relativeTo:@"superlayer"
attribute:kCAConstraintMaxX]];
}


@end


Stig -- http://code.brautaset.org http://devblog.brautaset.org




_______________________________________________ Do not post admin requests to the list. They will be ignored. Quartz-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.