Last active
December 16, 2015 23:59
-
-
Save petrpavlik/5517894 to your computer and use it in GitHub Desktop.
Example of how to use auto layout inside UITableViewCell
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
// | |
// PostCell.m | |
// Test | |
// | |
// Created by Petr Pavlik on 5/4/13. | |
// Copyright (c) 2013 Petr Pavlik. All rights reserved. | |
// | |
#import "PostCell.h" | |
@implementation PostCell | |
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier | |
{ | |
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; | |
if (self) { | |
// Initialization code | |
[self commonSetup]; | |
} | |
return self; | |
} | |
- (void)commonSetup { | |
UILabel* label = [[UILabel alloc] init]; | |
label.text = @"test test"; | |
//leave self.contentView.translatesAutoresizingMaskIntoConstraints to YES!!! | |
label.translatesAutoresizingMaskIntoConstraints = NO; //important | |
NSLayoutConstraint* c1 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1 constant:10]; | |
NSLayoutConstraint* c2 = [NSLayoutConstraint constraintWithItem:label attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1 constant:10]; | |
[self.contentView addConstraint:c1]; | |
[self.contentView addConstraint:c2]; | |
[self.contentView addSubview:label]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment