Last active
August 29, 2015 14:07
-
-
Save bartvandendriessche/7302ede6d9cbf8abf798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.m | |
// UIScrollViewAutoLayout | |
// | |
// Created by Bart Vandendriessche on 11/10/14. | |
// Copyright (c) 2014 Bart Vandendriessche. Copy freely and of your own will. | |
// | |
#import "ViewController.h" | |
@interface ViewController () | |
@property (nonatomic, strong) UIScrollView *scrollView; | |
@property (nonatomic, strong) UILabel *label1; | |
@property (nonatomic, strong) UILabel *label2; | |
@property (nonatomic, strong) UILabel *label3; | |
@property (nonatomic, strong) UILabel *label4; | |
@property (nonatomic, copy) NSString *shortText; | |
@property (nonatomic, copy) NSString *longText; | |
@end | |
CGFloat const StandardInset = 20; | |
@implementation ViewController | |
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
self.shortText = @"label 2"; | |
self.longText = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eros dui, lacinia eu mauris ac, bibendum imperdiet nulla. Nam sagittis tempus pellentesque. Ut at eleifend enim. Nam magna lorem, lobortis at ante non, facilisis fringilla elit. Nulla facilisi. Quisque efficitur felis at dui rutrum tempus. Cras sem urna, venenatis quis erat vitae, euismod mollis diam. Ut et rutrum nunc, eu consequat est. Proin imperdiet vestibulum neque, vitae suscipit diam porta eu. Nunc sagittis venenatis nibh, tristique venenatis nunc convallis eget. Aliquam in massa eget mauris aliquet mattis. In ac pulvinar ante. Curabitur ac imperdiet neque. Sed facilisis quis mi non porttitor. Donec commodo et est at accumsan. Fusce interdum, lacus nec lacinia venenatis, ante ante pretium nulla, vitae dapibus lorem nisi eu risus./n/nProin blandit lacus sed laoreet vulputate. Suspendisse libero orci, fermentum eu aliquet ac, aliquam vitae tellus. Ut vitae porttitor magna. Nullam id quam non mauris volutpat interdum sed eu ante. Mauris hendrerit fermentum nisl eu auctor. Phasellus luctus aliquam dolor et placerat. Maecenas scelerisque, dolor nec iaculis iaculis, augue nunc consequat velit, eu ullamcorper purus libero eget enim. Sed vel ipsum id neque fermentum lobortis id sit amet dui. Nulla lacinia enim non mattis dictum. Nullam dapibus lacinia tristique. Pellentesque id posuere mi, sed efficitur ex."; | |
} | |
return self; | |
} | |
- (void)viewDidLoad { | |
[super viewDidLoad]; | |
self.view.backgroundColor = [UIColor whiteColor]; | |
[self.scrollView addSubview:self.label1]; | |
[self.scrollView addSubview:self.label2]; | |
[self.scrollView addSubview:self.label3]; | |
[self.scrollView addSubview:self.label4]; | |
[self.view addSubview:self.scrollView]; | |
[self setupConstraints]; | |
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Toggle" style:UIBarButtonItemStylePlain target:self action:@selector(toggle:)]; | |
} | |
- (void)setupConstraints { | |
// pin the scrollview to its superview edges (make it fill the screen) | |
for (id attribute in @[@(NSLayoutAttributeTop), @(NSLayoutAttributeLeft), @(NSLayoutAttributeBottom), @(NSLayoutAttributeRight)]) { | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView | |
attribute:[attribute integerValue] | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.view | |
attribute:[attribute integerValue] | |
multiplier:1 | |
constant:0]]; | |
} | |
// pin label1 to the top side of the scrollview | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label1 | |
attribute:NSLayoutAttributeTop | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.scrollView | |
attribute:NSLayoutAttributeTop | |
multiplier:1 | |
constant:StandardInset]]; | |
UIView *topAnchor; | |
for (UILabel *label in @[self.label1, self.label2, self.label3, self.label4]) { | |
// pin all labels to the left side of the scrollview | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label | |
attribute:NSLayoutAttributeLeft | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.scrollView | |
attribute:NSLayoutAttributeLeft | |
multiplier:1 | |
constant:StandardInset]]; | |
// pin all labels to the right side of the scrollview | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label | |
attribute:NSLayoutAttributeRight | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.scrollView | |
attribute:NSLayoutAttributeRight | |
multiplier:1 | |
constant:StandardInset]]; | |
// assign a width constraint to the labels. This constraint can NOT be relative to the scrollView | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label | |
attribute:NSLayoutAttributeWidth | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.view | |
attribute:NSLayoutAttributeWidth | |
multiplier:1 | |
constant:-( 2 * StandardInset)]]; | |
// pin all labels to the bottom of the previous label | |
if (topAnchor) { | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:label | |
attribute:NSLayoutAttributeTop | |
relatedBy:NSLayoutRelationEqual | |
toItem:topAnchor | |
attribute:NSLayoutAttributeBottom | |
multiplier:1 | |
constant:StandardInset]]; | |
} | |
topAnchor = label; | |
} | |
// pin label4 to the bottom of the scrollview | |
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.label4 | |
attribute:NSLayoutAttributeBottom | |
relatedBy:NSLayoutRelationEqual | |
toItem:self.scrollView | |
attribute:NSLayoutAttributeBottom | |
multiplier:1 | |
constant:StandardInset]]; | |
} | |
#pragma mark - Actions | |
- (void)toggle:(id)sender { | |
if ([self.label2.text isEqualToString:self.shortText]) { | |
self.label2.text = self.longText; | |
} else { | |
self.label2.text = self.shortText; | |
} | |
[self.label2 invalidateIntrinsicContentSize]; | |
} | |
#pragma mark - Lazy subviews | |
- (UIScrollView *)scrollView { | |
if (_scrollView) { | |
return _scrollView; | |
} | |
_scrollView = [[UIScrollView alloc] init]; | |
_scrollView.translatesAutoresizingMaskIntoConstraints = NO; | |
_scrollView.backgroundColor = [UIColor yellowColor]; | |
return _scrollView; | |
} | |
- (UILabel *)label1 { | |
if (_label1) { | |
return _label1; | |
} | |
_label1 = [[UILabel alloc] init]; | |
_label1.translatesAutoresizingMaskIntoConstraints = NO; | |
_label1.text = @"Label 1"; | |
_label1.backgroundColor = [UIColor redColor]; | |
return _label1; | |
} | |
- (UILabel *)label2 { | |
if (_label2) { | |
return _label2; | |
} | |
_label2 = [[UILabel alloc] init]; | |
_label2.translatesAutoresizingMaskIntoConstraints = NO; | |
_label2.text = self.longText; | |
_label2.backgroundColor = [UIColor greenColor]; | |
_label2.numberOfLines = 0; | |
return _label2; | |
} | |
- (UILabel *)label3 { | |
if (_label3) { | |
return _label3; | |
} | |
_label3 = [[UILabel alloc] init]; | |
_label3.translatesAutoresizingMaskIntoConstraints = NO; | |
_label3.text = @"Label 3"; | |
_label3.backgroundColor = [UIColor purpleColor]; | |
return _label3; | |
} | |
- (UILabel *)label4 { | |
if (_label4) { | |
return _label4; | |
} | |
_label4 = [[UILabel alloc] init]; | |
_label4.translatesAutoresizingMaskIntoConstraints = NO; | |
_label4.text = @"Label 4"; | |
_label4.backgroundColor = [UIColor blueColor]; | |
return _label4; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment