Retina and VFL vs Programmatic Layout
Retina and VFL vs Programmatic Layout
- Subject: Retina and VFL vs Programmatic Layout
- From: Luther Baker <email@hidden>
- Date: Sat, 26 Jul 2014 10:44:54 -0500
In the following example, I'm displaying a child view in a container view
attached to a view controller's view property ... on a Retina iPhone
simulator.
EXPECTED: The VFL constraints cause the child to fill the screen whereas
UNEXPECTED: the programmatic constraints cause the child to fill exactly
1/4 of the screen (1/2 width, 1/2 height). The programmatic constraints
work if I set the multiplier=2.0 on each of those constraints.
I normally don't use width / height constraints this way but I'm surprised
by the outcome. Is this a Retina pixels/points thing or am I doing
something wrong?
Thanks,
-Luther
//
// 1. Fill the parent view with a simple, yellow container
//
_containerView = [[UIView alloc] init];
_containerView.backgroundColor = [UIColor yellowColor];
_containerView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:_containerView];
NSArray *cc = [NSLayoutConstraint constraintsWithVisualFormat:@
"V:|[container]|"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:@{
@"container" : _containerView }];
[self.view addConstraints:cc];
cc = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[container]|"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:@{ @"container" :
_containerView }];
[self.view addConstraints:cc];
//
// 2. Add a red child to the aforementioned container view
//
UIView *childView = [[UIView alloc] init];
childView.backgroundColor = [UIColor redColor];
childView.translatesAutoresizingMaskIntoConstraints = NO;
[_containerView addSubview:childView];
// WORKS AS EXPECTED
cc = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[child]|"
options:NSLayoutFormatAlignAllCenterX
metrics:nil
views:@{
@"child" : childView }];
[_containerView addConstraints:cc];
cc = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[child]|"
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:@{ @"child" :
childView }];
[_containerView addConstraints:cc];
//
// now, replace the VFL from above with this and the child view only fills
1/4 of the screen
//
NSLayoutConstraint *c = [NSLayoutConstraint constraintWithItem:childView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:_containerView
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0];
[_containerView addConstraint:c];
c = [NSLayoutConstraint constraintWithItem:childView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:_containerView
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0.0];
[_containerView addConstraint:c];
_______________________________________________
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