Skip to content

Instantly share code, notes, and snippets.

@petrpavlik
Last active December 16, 2015 23:59
Show Gist options
  • Save petrpavlik/5517894 to your computer and use it in GitHub Desktop.
Save petrpavlik/5517894 to your computer and use it in GitHub Desktop.
Example of how to use auto layout inside UITableViewCell
//
// 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