Skip to content

Instantly share code, notes, and snippets.

@rbarbera
Created May 21, 2014 08:08
Show Gist options
  • Save rbarbera/73dc24af0ad4477837bd to your computer and use it in GitHub Desktop.
Save rbarbera/73dc24af0ad4477837bd to your computer and use it in GitHub Desktop.
Autolayout bug with NSLayoutAttributeBaseLine
//
// ALEViewController.m
// FLKAutoLayoutExample
//
// Created by Rafa on 2014/05/21.
//
#import "ALEViewController.h"
#import "UIView+FLKAutoLayout.h"
@interface ALEViewController ()
@end
@implementation ALEViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self buildLayoutWithNSLayoutConstraints];
}
- (void)buildLayoutWithFLKAutoLayout
{
NSMutableArray* labels = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
UILabel* label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.backgroundColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:@"Label %d", i];
[self.view addSubview:label];
[labels addObject:label];
}
[labels[0] alignTopEdgeWithView:self.view predicate:@"40"];
[labels[0] constrainWidthToView:self.view predicate:@"*.5-20"];
[labels[0] alignLeadingEdgeWithView:self.view predicate:@"20"];
[UIView alignLeadingAndTrailingEdgesOfViews:labels];
[UIView spaceOutViewsVertically:labels predicate:@"20"];
NSMutableArray* textFields = [NSMutableArray array];
for (NSUInteger i = 0; i < labels.count; i++) {
UITextField* textField = [[UITextField alloc] init];
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.text = [NSString stringWithFormat:@"text field %d", i];
[self.view addSubview:textField];
[textFields addObject:textField];
}
[textFields[0] constrainLeadingSpaceToView:labels[0] predicate:@"20"];
[textFields[0] alignTrailingEdgeWithView:self.view predicate:@"-10"];
[UIView alignLeadingAndTrailingEdgesOfViews:textFields];
[UIView alignAttribute:NSLayoutAttributeBaseline ofViews:textFields toViews:labels predicate:nil];
NSLog(@"%s %@",__PRETTY_FUNCTION__,[self.view constraints]);
}
- (void)buildLayoutWithNSLayoutConstraints
{
UIView *previous = nil;
NSMutableArray* labels = [NSMutableArray array];
for (NSUInteger i = 0; i < 3; i++) {
UILabel* label = [[UILabel alloc] init];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.backgroundColor = [UIColor whiteColor];
label.text = [NSString stringWithFormat:@"Label %d", i];
[self.view addSubview:label];
[labels addObject:label];
if (!previous) {
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(40)-[label]"
options:kNilOptions
metrics:nil
views:NSDictionaryOfVariableBindings(label)]];
} else {
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[previous]-(20)-[label]"
options:kNilOptions
metrics:nil
views:NSDictionaryOfVariableBindings(label, previous)]];
}
previous = label;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(20)-[label]"
options:kNilOptions
metrics:nil
views:NSDictionaryOfVariableBindings(label)]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:0.5
constant:-20]];
}
NSMutableArray* textFields = [NSMutableArray array];
for (NSUInteger i = 0; i < labels.count; i++) {
UITextField* textField = [[UITextField alloc] init];
textField.translatesAutoresizingMaskIntoConstraints = NO;
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.text = [NSString stringWithFormat:@"text field %d", i];
[self.view addSubview:textField];
[textFields addObject:textField];
UIView *label = labels[i];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[label]-(20)-[textField]-(10)-|"
options:kNilOptions
metrics:nil
views:NSDictionaryOfVariableBindings(label,textField)]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:textField
attribute:NSLayoutAttributeBaseline
relatedBy:NSLayoutRelationEqual
toItem:label
attribute:NSLayoutAttributeBaseline
multiplier:1
constant:0]];
}
NSLog(@"%s %@",__PRETTY_FUNCTION__,[self.view constraints]);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment