Layout of custom UITableViewCell using Autolayout programmatically - ios

I have a custom UITableViewCell that works great on iPhone 5s, however since this projected started we now have to update it to support iPhone 6 en iPhone 6 Plus as well.
I've solved most of these upgrade issues using Autolayout in my .xib files. This Cell however does not have a .xib so I Have to do everything programmatically. For clarity I've temporarily drawn a border around my problematic label
This is how it looks on iPhone 5S
This is how it looks on iPhone 6
These tableviews are expandable/collapsable. In the screenshot you see it expanded.
This is the relevant code:
#import "LotsDetailTableViewCell.h"
#import "MSCellAccessory.h"
#interface LotsDetailTableViewCell ()
#property (nonatomic, strong) NSArray *data;
#property (nonatomic, strong) UIView *detailView;
#end
const float LABEL_HEIGHT = 22.0f;
#implementation LotsDetailTableViewCell
- (id)initWithData:(NSArray *)data reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
if (self) {
self.data = data;
[self setupDetailView];
self.textLabel.textColor = RGB(39, 143, 191);
self.textLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:16.0];
self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.clipsToBounds = true;
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
self.textLabel.frame = CGRectMake(self.textLabel.frame.origin.x,
self.textLabel.frame.origin.y,
self.textLabel.frame.size.width,
43.5f);
self.accessoryView.frame = CGRectMake(self.accessoryView.frame.origin.x, 6, 32, 32);
}
#pragma mark Detail View
- (void)setupDetailView
{
self.detailView = [[UIView alloc] init];
UILabel *previousLabel;
for (NSObject *detail in self.data) {
CGFloat frameY;
NSString *keyText;
NSString *valueText;
if([detail isKindOfClass:[NSString class]]){
keyText = #"";
valueText = (NSString *)detail;
} else if([detail isKindOfClass:[NSDictionary class]]){
keyText = ((NSDictionary *)detail)[#"key"];
valueText = [(NSDictionary *)detail objectForKey:#"value"];
}
if (previousLabel) {
frameY = previousLabel.frame.origin.y+previousLabel.frame.size.height;
} else {
frameY = self.frame.origin.y+44.0f+[self.data indexOfObject:detail]*LABEL_HEIGHT;
}
UILabel *keyLabel = [[UILabel alloc] initWithFrame:
CGRectMake(self.frame.size.width*.05, frameY, self.frame.size.width*.45, [self heightForValueLabel:valueText])
];
UILabel *valueLabel = [[UILabel alloc] initWithFrame:
CGRectMake(self.frame.size.width*.5, frameY, self.frame.size.width*.45, [self heightForValueLabel:valueText])
];
valueLabel.lineBreakMode = NSLineBreakByWordWrapping;
valueLabel.numberOfLines = 0;
keyLabel.lineBreakMode = NSLineBreakByWordWrapping;
keyLabel.numberOfLines = 0;
valueLabel.textAlignment = NSTextAlignmentRight;
keyLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:12.0];
valueLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
keyLabel.textColor = RGB(119, 119, 119);
valueLabel.textColor = RGB(39, 143, 191);
keyLabel.text = keyText;
valueLabel.text = valueText;
valueLabel.layer.borderColor = [RGB(178, 178, 178) CGColor];
valueLabel.layer.borderWidth = 1.0f;
[self.detailView addSubview:keyLabel];
[self.detailView addSubview:valueLabel];
previousLabel = valueLabel;
}
[self addSubview:self.detailView];
}
- (CGFloat)heightForValueLabel:(NSString *)text
{
return [[self class] heightForValueLabel:text width:self.frame.size.width*.5];
}
+ (CGFloat)heightForValueLabel:(NSString *)text width:(CGFloat)width
{
CGSize constrainedSize = CGSizeMake(width, CGFLOAT_MAX);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
return requiredHeight.size.height+5.0;
}
+ (CGFloat)heightForData:(NSArray*)data width:(CGFloat)width
{
CGFloat height = 0.0;
for (NSObject *detail in data) {
NSString *valueText;
if([detail isKindOfClass:[NSString class]]){
valueText = (NSString *)detail;
} else if([detail isKindOfClass:[NSDictionary class]]){
valueText = ((NSDictionary *)detail)[#"value"];
}
height = height + [self heightForValueLabel:valueText width:width];
}
return height;
}
#end
Most of the relevant drawing is performed in the setupDetailView method.
What I want: I want the label to be aligned to the right hand side of the tableviewcell to make best use of the available space.
What I tried: I've tried adding a NSLayoutConstraint programmatically in the setupDetailView using the following co
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:valueLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:1.0];
[valueLabel addConstraint:constraint];
[self needsUpdateConstraints];
but this has lead to run-time crashes.
If my problem can be easily solved without Autolayout I'm not opposed to that.
Edit: If I try to add the constraint, this is how it blows up:
2015-03-25 14:19:04.047 App[35277:10763464] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7faf5049a630 UILabel:0x7faf50499a50'8050'.trailing == LotsDetailTableViewCell:0x7faf50494840'LotDetailInfoCell'.right + 1>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2015-03-25 14:19:04.048 App[35277:10763464] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x7faf5049a630 UILabel:0x7faf50499a50'8050'.trailing == LotsDetailTableViewCell:0x7faf50494840'LotDetailInfoCell'.right + 1>
Container hierarchy:
<LotsDetailTableViewCell: 0x7faf50494840; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x7faf50494c50>>
| <UITableViewCellContentView: 0x7faf5049c070; frame = (0 0; 320 44); gestureRecognizers = <NSArray: 0x7faf504a2530>; layer = <CALayer: 0x7faf50455b60>>
View not found in container hierarchy: <UILabel: 0x7faf50499a50; frame = (156 44; 144 19.304); text = '8050'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7faf50499870>>
That view's superview: NO SUPERVIEW
2015-03-25 14:19:04.058 App[35277:10763464] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view. Does the constraint reference something from outside the subtree of the view? That's illegal. constraint:<NSLayoutConstraint:0x7faf5049a630 UILabel:0x7faf50499a50'8050'.trailing == LotsDetailTableViewCell:0x7faf50494840'LotDetailInfoCell'.right + 1> view:<LotsDetailTableViewCell: 0x7faf50494840; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x7faf50494c50>>'

Give leftConstarint to your label, after adding it as subView.
[_label setTranslatesAutoresizingMaskIntoConstraints:NO];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:_label
attribute:NSLayoutAttributeLeft
relatedBy:NSLayoutRelationEqual
toItem:[_label superview]
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:10];
[_label addConstraint:leftConstraint];
[_label superview] must be your [self view]. //Might be.
Hope, it helps. :)

When adding constraints between subview and superview, you have to add them to the superview:
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:valueLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:1.0];
[self addConstraint:constraint];
[self needsUpdateConstraints];

Related

UIButton embeded in UIVIew no responsible

I have a problem
i created 6 UIButtons put them in a UIView1
then put the UIView1 into another UIView2
then return UIView2 as UITableView.tableHeaderView
now the problem is when i touch the buttons
no responsiblity
here is some information
1. UIButtons UIView1 UIView2 fremes are these
2017-06-04 16:26:40.014 LOLHelper[8590:223132] BUT frame is :8.000000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.014 LOLHelper[8590:223132] BUT frame is :86.500000 8.000000 71.000000 18.000000
2017-06-04 16:26:40.014 LOLHelper[8590:223132] BUT frame is :165.500000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] BUT frame is :244.000000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] BUT frame is :322.500000 8.000000 71.000000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] BUT frame is :401.500000 8.000000 70.500000 18.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] SubView frame is: :-3.000000 0.000000 480.000000 20.000000
2017-06-04 16:26:40.015 LOLHelper[8590:223132] headerView frame is: :0.000000 0.000000 320.000000 20.000000
2.i turn on the userInteractionEnabled of UIButtons UIView1 UIView2 , u can see in the code :
#import "TableViewDataSource.h"
#implementation TableViewDataSource
#synthesize tableView;
#synthesize tableCell;
#synthesize LHfetchedResultsController;
#synthesize numberOfRows;
#synthesize dataTemp;
#synthesize paused;
#synthesize type;
#synthesize imgAche;
#synthesize cellHeight;
#synthesize subHeaderViewFrame;
-(id)initWithTableView:(UITableView *) tableView
{
self = [super init];
if (self) {
self.tableView = tableView;
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.tableHeaderView = [self setHeaderView];
}
return self;
}
-(UIView *)setHeaderView
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
UIView * container = [[UIView alloc] init];
container.frame = CGRectMake(0, 0, screenSize.width, 20);
container.userInteractionEnabled = YES;
UIView * headerView = [[UIView alloc] init];
headerView.frame = CGRectMake(0, 0, screenSize.width*1.5, 20);
headerView.tag = 01;
headerView.userInteractionEnabled = YES;
[container addSubview:headerView];
for (int i = 1; i < 7; i++) {
UIButton * button = [[UIButton alloc] init];
button.tag = i+10;
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
[button setTitle:[NSString stringWithFormat:#"but0%d",i] forState:UIControlStateNormal];
[button setTitle:[NSString stringWithFormat:#"but0%d s",i] forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.translatesAutoresizingMaskIntoConstraints = NO;
[button setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
button.enabled = YES;
button.userInteractionEnabled = YES;
[headerView addSubview:button];
}
UIButton *button01 = [headerView viewWithTag:11];
UIButton *button02 = [headerView viewWithTag:12];
UIButton *button03 = [headerView viewWithTag:13];
UIButton *button04 = [headerView viewWithTag:14];
UIButton *button05 = [headerView viewWithTag:15];
UIButton *button06 = [headerView viewWithTag:16];
NSDictionary * views = NSDictionaryOfVariableBindings(button01,button02,button03,button04,button05,button06);
NSString * vflV1 = #"V:|-1-[button01]-1-|";
NSString * vflV2 = #"V:|-1-[button02]-1-|";
NSString * vflV3 = #"V:|-1-[button03]-1-|";
NSString * vflV4 = #"V:|-1-[button04]-1-|";
NSString * vflV5 = #"V:|-1-[button05]-1-|";
NSString * vflV6 = #"V:|-1-[button06]-1-|";
NSString * vflH1 = #"H:|-[button01]-[button02(==button01)]-[button03(==button01)]-[button04(==button01)]-[button05(==button01)]-[button06(==button01)]-|";
NSArray * constraintsV1 = [NSLayoutConstraint constraintsWithVisualFormat:vflV1 options:0 metrics:NULL views:views];
NSArray * constraintsV2 = [NSLayoutConstraint constraintsWithVisualFormat:vflV2 options:0 metrics:NULL views:views];
NSArray * constraintsV3 = [NSLayoutConstraint constraintsWithVisualFormat:vflV3 options:0 metrics:NULL views:views];
NSArray * constraintsV4 = [NSLayoutConstraint constraintsWithVisualFormat:vflV4 options:0 metrics:NULL views:views];
NSArray * constraintsV5 = [NSLayoutConstraint constraintsWithVisualFormat:vflV5 options:0 metrics:NULL views:views];
NSArray * constraintsV6 = [NSLayoutConstraint constraintsWithVisualFormat:vflV6 options:0 metrics:NULL views:views];
NSArray * constraintsH1 = [NSLayoutConstraint constraintsWithVisualFormat:vflH1 options:0 metrics:NULL views:views];
[headerView addConstraints:constraintsV1];
[headerView addConstraints:constraintsV2];
[headerView addConstraints:constraintsV3];
[headerView addConstraints:constraintsV4];
[headerView addConstraints:constraintsV5];
[headerView addConstraints:constraintsV6];
[headerView addConstraints:constraintsH1];
return container;
}
i have not find the reason
anyone could help me?
i also add some UISwipeGestureRecognizer into UIView2
thank u
Thank u Guys
i got the solution
reason is that
[button addTarget:NULL action:#selector(click:) forControlEvents:UIControlEventTouchUpInside];
i set target NULL, and write click: in the same file, but actually i get the table from the main controller, so when i click button, xcode will find click: in the main controller, then can not find, so no responsibility.
after i set target self or write click: in main controller
it works

View not found in container hierarchy crash while adding View constraints programatically

Although the crash message is very clear but I have checked my code and everything seems to be looking fine. My only worry is because I am setting constraints on a view which is being set as accessory view for keyboard, could that be a reason why entire view hierarchy is not set properly.
Here is what I am doing:
Step 1: Create a custom accessory view (a number bar from 0 to 9) and put it on top of regular keyboard.
MyAccessoryView *anAccessoryView = [[MyAccessoryView alloc] initWithFrame:CGRectMake(0, 0, 320, 46.0f)];
anAccessoryView.delegate = self;
self.textField.inputAccessoryView = anAccessoryView;
Step 2: In custom accessory view (subview of UIView), add buttons and related handling. Add them to super view and then call a method after a delay of 1 second to add constraints.
- (id)initWithFrame:(CGRect)iFrame {
if (self = [super initWithFrame:iFrame]) {
[self refresh];
self.autoresizingMask = UIViewAutoresizingFlexibleWidth;
}
[self setExclusiveTouch:YES];
return self;
}
- (void)refresh {
if (!self.layoutController) {
self.layoutController = [[MyAccessoryViewLayoutController alloc] init];
}
self.backgroundColor = [self.layoutController colorForNumericInputAccessory:self];
self.userInteractionEnabled = YES;
NSMutableArray *buttonsArray = [NSMutableArray arrayWithCapacity:10];
// Adding number keys to the accessory view and setting thier properties
for (NSInteger i = 1; i < 11; i++) {
MyAccessoryViewNumericButton *aKeyboardButton = [MyAccessoryViewNumericButton buttonWithType:UIButtonTypeCustom];
aKeyboardButton.tag = i;
aKeyboardButton.frame = CGRectMake(0, 0, 32.0, 44);
[aKeyboardButton setTitle: [NSString stringWithFormat:#"%ld", (long)i % 10] forState:UIControlStateNormal];
NSString *aButtonImageTitle = [self.layoutController buttonNormalStateImageNameForIndex:(i % 10) forNumericInputAccessory:self];
[aKeyboardButton setBackgroundImage:[UIImage imageNamed:aButtonImageTitle] forState:UIControlStateNormal];
aKeyboardButton.backgroundColor = [UIColor clearColor];
[aKeyboardButton setTitleColor:[UIColor blackColor] forState: UIControlStateNormal];
[aKeyboardButton setExclusiveTouch:YES];
aKeyboardButton.titleLabel.font = [self.layoutController fontForButtonTitlesForNumericInputAccessory:self];
if (self.layoutController.interfaceIdiom == UIUserInterfaceIdiomPhone) {
aKeyboardButton.titleLabel.textAlignment = NSTextAlignmentCenter;
} else {
NSString *aButtonHighlightedImageTitle = [self.layoutController buttonHighlightedStateImageNameForIndex:(i % 10) forNumericInputAccessory:self];
[aKeyboardButton setBackgroundImage:[UIImage imageNamed:aButtonHighlightedImageTitle] forState:UIControlStateHighlighted];
}
aKeyboardButton.hidden = NO;
aKeyboardButton.enabled = YES;
aKeyboardButton.tag = i;
[aKeyboardButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:aKeyboardButton];
[buttonsArray addObject:aKeyboardButton];
if (self.layoutController.interfaceIdiom == UIUserInterfaceIdiomPhone) {
UIImage *aPressedButtonImage = [UIImage imageNamed:[self.layoutController buttonHighlightedStateImageNameForIndex:(i % 10) forNumericInputAccessory:self]];
CGRect aPressedButtonViewFrame = [self.layoutController frameForPressedButtonViewWithIndex:(i % 10) forNumericInputAccessory:self];
UIView *aPressedButtonView = [[UIView alloc] initWithFrame:aPressedButtonViewFrame];
aPressedButtonView.backgroundColor = [UIColor clearColor];
aPressedButtonView.userInteractionEnabled = YES;
aPressedButtonView.hidden = YES;
UIImageView *aPressedButtonImageView = [[UIImageView alloc] initWithImage:aPressedButtonImage];
aPressedButtonImageView.frame = CGRectMake(0.0, 0.0, aPressedButtonViewFrame.size.width, aPressedButtonViewFrame.size.height);
aPressedButtonImageView.contentMode = UIViewContentModeBottom;
aPressedButtonImageView.backgroundColor = [UIColor clearColor];
CGRect aPressedButtonLabelFrame = [self.layoutController frameForPressedButtonLabelWithIndex:(i % 10) forNumericInputAccessory:self];
UILabel *aPressedButtonLabel = [[UILabel alloc] initWithFrame:aPressedButtonLabelFrame];
aPressedButtonLabel.backgroundColor = [UIColor clearColor];
aPressedButtonLabel.text = [NSString stringWithFormat: #"%ld", (long)i % 10];
aPressedButtonLabel.textAlignment = NSTextAlignmentCenter;
aPressedButtonView.tag = i + 100;
aPressedButtonLabel.font = [UIFont fontWithName: #"Helvetica-Bold" size: 44.0f];
aPressedButtonLabel.shadowColor = [UIColor colorWithRed: 0.95f green: 0.95f blue: 0.95f alpha: 1.0f];
aPressedButtonLabel.shadowOffset = CGSizeMake(0.0f, 0.75f);
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation])) {
aPressedButtonImageView.frame = CGRectMake(aPressedButtonImageView.frame.origin.x, aPressedButtonImageView.frame.origin.y - 2.0f, aPressedButtonImageView.frame.size.width, aPressedButtonImageView.frame.size.height);
}
[aPressedButtonView addSubview:aPressedButtonImageView];
[aPressedButtonView addSubview:aPressedButtonLabel];
[self addSubview:aPressedButtonView];
}
}
CALayer *aLightGreyBorder = [CALayer layer];
aLightGreyBorder.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y + 1.0f, self.bounds.size.width, 1.0f);
UIColor *aColor = [UIColor colorWithRed:178.0f/255.0f green:184.0f/255.0f blue:191.0f/255.0f alpha:1.0f];
aLightGreyBorder.backgroundColor = [aColor CGColor];
[self.layer insertSublayer:aLightGreyBorder atIndex:0];
CALayer *aDarkGreyBorder = [CALayer layer];
aDarkGreyBorder.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, self.bounds.size.width, 1.0f);
UIColor *aSecondColor;
aSecondColor = [UIColor colorWithRed:100.0f/255.0f green:100.0f/255.0f blue:100.0f/255.0f alpha:1.0f];
aDarkGreyBorder.backgroundColor = [aSecondColor CGColor];
[self.layer insertSublayer:aDarkGreyBorder atIndex:0];
if (self.layoutController.interfaceIdiom == UIUserInterfaceIdiomPhone) {
for (UIView *aPressedButtonView in [self subviews]) {
if ([aPressedButtonView isMemberOfClass:[UIView class]]) {
[self bringSubviewToFront:aPressedButtonView];
}
}
}
// Now lets add constaints
[self performSelector:#selector(addConstraintsForButtons:) withObject:buttonsArray afterDelay:1.0];
}
Step 3: Add constraints to all buttons (0 to 9). Here, I am showing only first 2 buttons.
- (void)addConstraintsForButtons:(NSMutableArray *)iButtons {
// First Button
MyAccessoryViewNumericButton *firstButton = (MyAccessoryViewNumericButton *)iButtons[0];
// Left edge constraint for First button
NSLayoutConstraint *leading =[NSLayoutConstraint
constraintWithItem:firstButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:4.f];
[self addConstraint:leading];
// Middle in the super view
NSLayoutConstraint *vertical =[NSLayoutConstraint
constraintWithItem:firstButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0];
[self addConstraint:vertical];
// Width-Height Constraint for First button
[self addWHContraintForButton:firstButton];
// Second Button
MyAccessoryViewNumericButton *secondButton = (MyAccessoryViewNumericButton *)iButtons[1];
// Left edge constraint for First button
NSLayoutConstraint *secondLeading =[NSLayoutConstraint
constraintWithItem:secondButton
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:firstButton
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:4.f];
[secondButton addConstraint:secondLeading];
// Middle in the super view
NSLayoutConstraint *secondVertical =[NSLayoutConstraint
constraintWithItem:secondButton
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:firstButton
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0];
[secondButton addConstraint:secondVertical];
// Width-Height Constraint for Second button
[self addWHContraintForButton:secondButton];
// Add constaints to rest of the buttons...
}
- (void)addWHContraintForButton:(MyAccessoryViewNumericButton *)iButton {
CGFloat buttonWidth = 32.0f;
CGFloat buttonHeight = 44.0f;
// Height Constraint
NSLayoutConstraint *height = [NSLayoutConstraint
constraintWithItem:iButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:0
constant:buttonHeight];
// Width Constraint
NSLayoutConstraint *width = [NSLayoutConstraint
constraintWithItem:iButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:0
multiplier:0
constant:buttonWidth];
[iButton addConstraint:height];
[iButton addConstraint:width];
}
But when I run this code, it crashes with following message:
View not found in container hierarchy: <MyAccessoryViewNumericButton: 0x7fb506036f90; baseClass = UIButton; frame = (0 0; 32 44); opaque = NO; tag = 1; layer = <CALayer: 0x7fb506039c70>>
That view's superview: <MyAccessoryView: 0x7fb503ddfb50; frame = (0 0; 320 46); autoresize = W; layer = <CALayer: 0x7fb503dc1fe0>>
This is what I see when debug this (tried by setting constraints in keyboardDidShow: and didMoveToSuperview methods):
(lldb) po [secondButton superview]
<MyAccessoryView: 0x7fcd6d04f6c0; frame = (0 0; 320 46); autoresize = W; layer = <CALayer: 0x7fcd6adf5ed0>>
(lldb) po [[secondButton superview] superview]
<UIInputSetHostView: 0x7fcd6d0346c0; frame = (0 522; 320 262); layer = <CALayer: 0x7fcd6adef270>>
(lldb) po [[[secondButton superview] superview] superview]
<UIInputSetContainerView: 0x7fcd6d014000; frame = (0 0; 320 568); layer = <CALayer: 0x7fcd6add78d0>>
(lldb) po [[[[secondButton superview] superview] superview] superview]
<UITextEffectsWindow: 0x7fcd6d033c30; frame = (0 0; 320 568); opaque = NO; autoresize = W+H; layer = <UIWindowLayer: 0x7fcd6d034250>>
(lldb) po [[[[[secondButton superview] superview] superview] superview] superview]
0x0000000000000000
Although view has a parent. Can someone please advise here.

Xcode Table Cell Image Overlap

I'm learning Objective C & working on an Instagram clone for a class assignment. Currently I have a problem with auto layout. Specifically, images & comments are overlapping on each other:
I believe the problem is in the auto layout:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.mediaImageView = [[UIImageView alloc] init];
self.usernameAndCaptionLabel = [[UILabel alloc] init];
self.usernameAndCaptionLabel.numberOfLines = 0;
self.commentLabel = [[UILabel alloc] init];
self.commentLabel.numberOfLines = 0;
for (UIView *view in #[self.mediaImageView, self.usernameAndCaptionLabel, self.commentLabel]) {
[self.contentView addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
}
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(_mediaImageView, _usernameAndCaptionLabel, _commentLabel);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_mediaImageView]|" options:kNilOptions metrics:nil views:viewDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_usernameAndCaptionLabel]|" options:kNilOptions metrics:nil views:viewDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_commentLabel]|" options:kNilOptions metrics:nil views:viewDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_mediaImageView][_usernameAndCaptionLabel][_commentLabel]"
options:kNilOptions
metrics:nil
views:viewDictionary]];
self.imageHeightConstraint = [NSLayoutConstraint constraintWithItem:_mediaImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
self.usernameAndCaptionLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:_usernameAndCaptionLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
self.commentLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:_commentLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
[self.contentView addConstraints:#[self.imageHeightConstraint, self.usernameAndCaptionLabelHeightConstraint, self.commentLabelHeightConstraint]]; }
return self;
}
I've gone through the class tutorial several time and have researched iOS SDK Table Cell Overlapping but have not been able to find a suitable answer.
Here is the entire code:
#import "AppDelegate.h"
#import "UITableViewCell+BLCMediaTableViewCell.h"
#import "NSObject+BLCMedia.h"
#import "NSObject+BLCComment.h"
#import "NSObject+BLCUser.h"
//#implementation UITableViewCell (BLCMediaTableViewCell)
#interface BLCMediaTableViewCell() {
BLCMedia* _mediaItem;
}
#property (nonatomic, strong) UIImageView *mediaImageView;
#property (nonatomic, strong) UILabel *usernameAndCaptionLabel;
#property (nonatomic, strong) UILabel *commentLabel;
// Assignment: Added auto layout
#property (nonatomic, strong) NSLayoutConstraint *imageHeightConstraint;
#property (nonatomic, strong) NSLayoutConstraint *usernameAndCaptionLabelHeightConstraint;
#property (nonatomic, strong) NSLayoutConstraint *commentLabelHeightConstraint;
#end
static UIFont *lightFont;
static UIFont *boldFont;
static UIColor *usernameLabelGray;
static UIColor *commentLabelGray;
static UIColor *linkColor;
static NSParagraphStyle *paragraphStyle;
#implementation BLCMediaTableViewCell : UITableViewCell
- (void) layoutSubviews
{
[super layoutSubviews];
// Before layout, calculate the intrinsic size of the labels (the size they "want" to be), and add 20 to the height for some vertical padding.
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 100, 100);
CGSize maxSize = CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX);
CGSize usernameLabelSize = [self.usernameAndCaptionLabel sizeThatFits:maxSize];
CGSize commentLabelSize = [self.commentLabel sizeThatFits:maxSize];
self.usernameAndCaptionLabelHeightConstraint.constant = usernameLabelSize.height + 20;
self.commentLabelHeightConstraint.constant = commentLabelSize.height + 20;
// Hide the line between cells
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.bounds));
}
+ (void)load {
lightFont = [UIFont fontWithName:#"HelveticaNeue-Thin" size:11];
boldFont = [UIFont fontWithName:#"HelveticaNeue-Bold" size:11];
usernameLabelGray = [UIColor colorWithRed:0.933 green:0.933 blue:0.933 alpha:1]; /*#eeeeee*/
commentLabelGray = [UIColor colorWithRed:0.898 green:0.898 blue:0.898 alpha:1]; /*#e5e5e5*/
linkColor = [UIColor colorWithRed:0.345 green:0.314 blue:0.427 alpha:1]; /*#58506d*/
NSMutableParagraphStyle *mutableParagraphStyle = [[NSMutableParagraphStyle alloc] init];
mutableParagraphStyle.headIndent = 20.0;
mutableParagraphStyle.firstLineHeadIndent = 20.0;
mutableParagraphStyle.tailIndent = -20.0;
mutableParagraphStyle.paragraphSpacingBefore = 5;
paragraphStyle = mutableParagraphStyle;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.mediaImageView = [[UIImageView alloc] init];
self.usernameAndCaptionLabel = [[UILabel alloc] init];
self.usernameAndCaptionLabel.numberOfLines = 0;
self.commentLabel = [[UILabel alloc] init];
self.commentLabel.numberOfLines = 0;
for (UIView *view in #[self.mediaImageView, self.usernameAndCaptionLabel, self.commentLabel]) {
[self.contentView addSubview:view];
view.translatesAutoresizingMaskIntoConstraints = NO;
}
NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(_mediaImageView, _usernameAndCaptionLabel, _commentLabel);
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_mediaImageView]|" options:kNilOptions metrics:nil views:viewDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_usernameAndCaptionLabel]|" options:kNilOptions metrics:nil views:viewDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[_commentLabel]|" options:kNilOptions metrics:nil views:viewDictionary]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_mediaImageView][_usernameAndCaptionLabel][_commentLabel]"
options:kNilOptions
metrics:nil
views:viewDictionary]];
self.imageHeightConstraint = [NSLayoutConstraint constraintWithItem:_mediaImageView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
self.usernameAndCaptionLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:_usernameAndCaptionLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
self.commentLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:_commentLabel
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1
constant:100];
[self.contentView addConstraints:#[self.imageHeightConstraint, self.usernameAndCaptionLabelHeightConstraint, self.commentLabelHeightConstraint]]; }
return self;
}
- (NSAttributedString *) usernameAndCaptionString {
CGFloat usernameFontSize = 15;
// Make a string that says "username caption text"
NSString *baseString = [NSString stringWithFormat:#"%# %#", self.mediaItem.user.userName, self.mediaItem.caption];
// Make an attributed string, with the "username" bold
NSMutableAttributedString *mutableUsernameAndCaptionString = [[NSMutableAttributedString alloc] initWithString:baseString attributes:#{NSFontAttributeName : [lightFont fontWithSize:usernameFontSize], NSParagraphStyleAttributeName : paragraphStyle}];
NSRange usernameRange = [baseString rangeOfString:self.mediaItem.user.userName];
[mutableUsernameAndCaptionString addAttribute:NSFontAttributeName value:[boldFont fontWithSize:usernameFontSize] range:usernameRange];
[mutableUsernameAndCaptionString addAttribute:NSForegroundColorAttributeName value:linkColor range:usernameRange];
return mutableUsernameAndCaptionString;
}
- (void) setMediaItem:(BLCMedia *)mediaItem {
_mediaItem = mediaItem;
self.mediaImageView.image = _mediaItem.image;
self.usernameAndCaptionLabel.attributedText = [self usernameAndCaptionString];
self.commentLabel.attributedText = [self commentString];
self.imageHeightConstraint.constant = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);
}
//Added
- (CGSize) sizeOfString:(NSAttributedString *)string {
CGSize maxSize = CGSizeMake(CGRectGetWidth(self.contentView.bounds) - 40, 0.0);
CGRect sizeRect = [string boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
sizeRect.size.height += 20;
sizeRect = CGRectIntegral(sizeRect);
return sizeRect.size;
}
- (NSAttributedString *) commentString {
NSMutableAttributedString *commentString = [[NSMutableAttributedString alloc] init];
for (BLCComment *comment in self.mediaItem.comments) {
// Make a string that says "username comment text" followed by a line break
NSString *baseString = [NSString stringWithFormat:#"%# %#\n", comment.from.userName, comment.text];
// Make an attributed string, with the "username" bold
NSMutableAttributedString *oneCommentString = [[NSMutableAttributedString alloc] initWithString:baseString attributes:#{NSFontAttributeName : lightFont, NSParagraphStyleAttributeName : paragraphStyle}];
NSRange usernameRange = [baseString rangeOfString:comment.from.userName];
[oneCommentString addAttribute:NSFontAttributeName value:boldFont range:usernameRange];
[oneCommentString addAttribute:NSForegroundColorAttributeName value:linkColor range:usernameRange];
[commentString appendAttributedString:oneCommentString];
}
return commentString;
}
+ (CGFloat) heightForMediaItem:(BLCMedia *)mediaItem width:(CGFloat)width {
// Make a cell
BLCMediaTableViewCell *layoutCell = [[BLCMediaTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"layoutCell"];
[layoutCell setNeedsLayout];
[layoutCell layoutIfNeeded];
// Get the actual height required for the cell
return CGRectGetMaxY(layoutCell.commentLabel.frame);
}
#end
So I figured out what I was doing wrong. My layoutSubViews were incorrect. I've included a screenshot of the bad version & the corrected version in my public Dropbox. I hope this can help someone else later down the road.
The original code was:
- (void) layoutSubviews
{
[super layoutSubviews];
// Before layout, calculate the intrinsic size of the labels (the size they "want" to be), and add 20 to the height for some vertical padding.
self.imageView.contentMode = UIViewContentModeScaleAspectFill;
self.imageView.frame = CGRectMake(self.imageView.frame.origin.x, self.imageView.frame.origin.y, 100, 100);
CGSize maxSize = CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX);
CGSize usernameLabelSize = [self.usernameAndCaptionLabel sizeThatFits:maxSize];
CGSize commentLabelSize = [self.commentLabel sizeThatFits:maxSize];
self.usernameAndCaptionLabelHeightConstraint.constant = usernameLabelSize.height + 20;
self.commentLabelHeightConstraint.constant = commentLabelSize.height + 20;
// Hide the line between cells
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.bounds));
}
It should have read:
- (void) layoutSubviews
{
[super layoutSubviews];
if (self.mediaItem == nil){
return;
}
// Before layout, calculate the intrinsic size of the labels (the size they "want" to be), and add 20 to the height for some vertical padding.
CGSize maxSize = CGSizeMake(CGRectGetWidth(self.bounds), CGFLOAT_MAX);
CGSize usernameLabelSize = [self.usernameAndCaptionLabel sizeThatFits:maxSize];
CGSize commentLabelSize = [self.commentLabel sizeThatFits:maxSize];
self.usernameAndCaptionLabelHeightConstraint.constant = usernameLabelSize.height + 20;
self.commentLabelHeightConstraint.constant = commentLabelSize.height + 20;
if (_mediaItem.image)
{
self.imageHeightConstraint.constant = self.mediaItem.image.size.height / self.mediaItem.image.size.width * CGRectGetWidth(self.contentView.bounds);
}
else
{
self.imageHeightConstraint.constant = 100;
}
//Hide the line between cells
self.separatorInset = UIEdgeInsetsMake(0, 0, 0, CGRectGetWidth(self.bounds));
}

UILabel/Content not appearing in UIScrollView

been trying to figure this out for a while. I'm tapping on the CNPPopupController and I'm trying to add a UIScrollView into the UIView.
In the scroll view I will be adding the content, I have tried so far but the content does not seem to be appearing in the UIScrollView. I am not sure what I'm doing wrongly, can anyone please guide me?
//The content view that will house labels and a UIScrollView
self.contentView = [[UIView alloc] init];
[self.contentView setTranslatesAutoresizingMaskIntoConstraints:NO];
self.contentView.clipsToBounds = YES;
self.contentView.backgroundColor = self.theme.backgroundColor;
self.contentView.layer.cornerRadius = self.theme.popupStyle == CNPPopupStyleCentered ? self.theme.cornerRadius : 0.0f;
[self.maskView addSubview:self.contentView];//The UI ScrollView
self.scrollView = [[UIScrollView alloc] init];
//self.scrollView.backgroundColor= self.theme.backgroundColor;
self.scrollView.frame = CGRectMake(50, 50, 200, 100);
self.scrollView.layer.cornerRadius = self.theme.popupStyle == CNPPopupStyleCentered ? self.theme.cornerRadius : 0.0f;
self.scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
self.scrollView.scrollEnabled = YES;
if (self.popupTitle) {
UILabel *title = [self multilineLabelWithAttributedString:self.popupTitle];
[self.contentView addSubview:title];
}
//Adding items in the UISCrollView
if (self.contents) {
for (NSObject *content in self.contents) {
if ([content isKindOfClass:[NSAttributedString class]]) {
UILabel *label = [self multilineLabelWithAttributedString:(NSAttributedString *)content];
[label sizeToFit];
CGSize scrollViewContentSize = CGSizeMake(self.contentView.frame.size.width, self.contentView.frame.size.height+500);
[self.scrollView setContentSize:scrollViewContentSize];
[self.scrollView addSubview:label];
}
else if ([content isKindOfClass:[UIImage class]]) {
UIImageView *imageView = [self centeredImageViewForImage:(UIImage *)content];
[imageView sizeToFit];
CGSize scrollViewContentSize = CGSizeMake(self.contentView.frame.size.width, self.contentView.frame.size.height+500);
[self.scrollView setContentSize:scrollViewContentSize];
[self.scrollView addSubview:imageView];
[self.scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationLessThanOrEqual toItem:imageView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0]];
}
}
[self.contentView addSubview:self.scrollView];
}

Using autolayout in UIScrollView subviews (programmatically)

First of all I've been reading all questions similar to this one, but with no success, so finally I'll try asking my specific case.
I have a UIScrollView that I fill with components programmatically this way:
- (void) fillScrollView(int numItems)
{
float posY = 10.0;
for(int i = 0; i < numItems; i++)
{
UIImageView *leftImg = [[UIImageView alloc] init];
[leftImg setTranslatesAutoresizingMaskIntoConstraints:NO];
[leftImg setFrame:CGRectMake(10.0, posY, 20.0, 20.0)];
[leftImg setImage:[UIImage imageNamed:#"img1"]];
UILabel *txtLb = [[UILabel alloc] init];
[txtLb setTranslatesAutoresizingMaskIntoConstraints:NO];
[txtLb setFont:[UIFont systemFontOfSize:15.0]];
[txtLb setNumberOfLines:0];
[txtLb setLineBreakMode:NSLineBreakByWordWrapping];
[txtLb setFrame:CGRectMake(40.0, posY, 240.0, 20.0)];
NSString *data = #"This is my example text, it could be longer.";
[txtLb setText:data];
CGRect paragraphRect = [dato boundingRectWithSize:CGSizeMake(txtLb.frame.size.width, 9999.0)
options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
attributes:#{NSFontAttributeName: txtLb.font}
context:nil];
float height = paragraphRect.size.height;
CGRect frame = txtLb.frame;
frame.size.height = ceil(height);
[txtLb setFrame:frame];
UIImageView *rightImg = [[UIImageView alloc] init];
[rightImg setTranslatesAutoresizingMaskIntoConstraints:NO];
[rightImg setFrame:CGRectMake(290.0, posY, 20.0, 20.0)];
[rightImg setImage:[UIImage imageNamed:#"img2"]];
[_scrollV addSubview:leftImg];
[_scrollV addSubview:txtLb];
[_scrollV addSubview:rightImg];
height = height + 20.0;
if(height < 40.0) height = 40.0;
posY = posY + height;
}
[_scrollV setContentSize:CGSizeMake(_scrollV.frame.size.width, posY)];
}
I would like that theis constraints were:
H:|-10-[leftImg(20)]-10-[txtLb]-10-[rightImg(20)]-10-|
And vertically each row has a space of 10px of vertical separation from the row above.
I've tried using constraintWithItem but I'm confused about how to use it in this case.
Any help would be very appreciated. Kind regards!
EDIT
Following Zhang's suggestion I've put the 3 components inside a UIView. This way I can use autolayout between them without problems and everything is in the correct position inside each UIView.
However I'm still having problems using autolayout between UIViews inside the loop. I'm doing this:
All blocks have no left/right margin with the UIScrollView.
// 0px to the left of the UIScrollView
NSLayoutConstraint *constraintLeft = [NSLayoutConstraint constraintWithItem:block
attribute:NSLayoutAttributeLeftMargin
relatedBy:NSLayoutRelationEqual
toItem:_scrollV
attribute:NSLayoutAttributeRightMargin
multiplier:1.0
constant:0.0];
// 0px to the right of the UIScrollView
NSLayoutConstraint *constraintRight = [NSLayoutConstraint constraintWithItem:block
attribute:NSLayoutAttributeRightMargin
relatedBy:NSLayoutRelationEqual
toItem:_scrollV
attribute:NSLayoutAttributeLeftMargin
multiplier:1.0
constant:0.0];
[_scrollV addConstraint:constraintLeft];
[_scrollV addConstraint:constraintRight];
Regarding to the vertical separation between blocks, when the UIView is the first block of the UIScrollView:
// 10px below UIScrollView top
NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:block
attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationEqual
toItem:_scrollV
attribute:NSLayoutAttributeBottomMargin
multiplier:1.0
constant:10.0];
[_scrollV addConstraint:constraintTop];
And when the UIView has any block above it:
// 10px below previous block
NSLayoutConstraint *constraintTop = [NSLayoutConstraint constraintWithItem:block
attribute:NSLayoutAttributeTopMargin
relatedBy:NSLayoutRelationEqual
toItem:previousBlock
attribute:NSLayoutAttributeBottomMargin
multiplier:1.0
constant:10.0];
[_scrollV addConstraint:constraintTop];
This shows all blocks without vertical separation, all in the same Y position, and also give errors applying constraints.
I'm sure I'm not using the right way constraintWithItem, but I can not find examples for this use.
It seems like you're trying to reinvent the wheel mate. You probably should be using UICollectionView or UITableView instead of UIScrollView and manually adding your cells.
Anyhow, for your scrollView method, one way you can implement it is like this:
ViewController Header File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIView *contentView;
#end
ViewController Implementation File
#import "ViewController.h"
#import "Cell.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initViews];
[self initConstraints];
[self fillScrollView:15];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
-(void)initViews
{
self.scrollView = [[UIScrollView alloc] init];
// ------------------------------------------------------------------
// This content view will be the only child view of scrollview
// ------------------------------------------------------------------
self.contentView = [[UIView alloc] init];
// add content view to scrollview now
[self.scrollView addSubview:self.contentView];
// add scrollview to main view
[self.view addSubview:self.scrollView];
}
-(void)initConstraints
{
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"scrollView": self.scrollView,
#"contentView": self.contentView
};
// setup scrollview constraints
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[scrollView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[scrollView]|" options:0 metrics:nil views:views]];
// ---------------------------------------
// setup content view constraint
//
// note: need to pin all four side of
// contentView to scrollView
// ---------------------------------------
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[contentView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[contentView]|" options:0 metrics:nil views:views]];
}
-(void)fillScrollView:(int) numItems
{
// clear any previous cells before adding new ones
[self.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
// Need to construct the layout visual format string
NSMutableString *strVerticalConstraint = [[NSMutableString alloc] init];
[strVerticalConstraint appendString:#"V:|"];
// this dictionary will hold all the key-value pair that identifies all the subviews
NSMutableDictionary *subviews = [[NSMutableDictionary alloc] init];
for(int i = 0; i < numItems; i++)
{
Cell *cell = [[Cell alloc] init];
// customize the cell's appearance here
cell.leftImage.image = [UIImage imageNamed:#"leftImage.png"];
cell.textLabel.text = #"This is my example text, it could be longer.";
cell.rightImage.image = [UIImage imageNamed:#"rightImage.png"];
[self.contentView addSubview:cell];
cell.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"cell": cell
};
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[cell]|" options:0 metrics:nil views:views]];
// prevent cell's width to extend beyond screen width
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:cell attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:self.view.bounds.size.width]];
// cell name
NSString *cellName = [[NSString alloc] initWithFormat:#"cell%d", i];
// construct each cell's vertical constraint to add it strVerticalConstraint
NSString *viewName = nil;
if(i < numItems - 1)
{
viewName = [[NSString alloc] initWithFormat:#"[%#(50)]-10-", cellName];
}
else
{
viewName = [[NSString alloc] initWithFormat:#"[%#(50)]", cellName];
}
[strVerticalConstraint appendString:viewName];
// add cell name to dictionary
[subviews setValue:cell forKey:cellName];
}
[strVerticalConstraint appendString:#"|"];
NSLog(#"strVerticalConstraint: \n%#", strVerticalConstraint);
// Finally, use the long vertical constraint string
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:strVerticalConstraint options:0 metrics:nil views:subviews]];
}
#end
Cell Header File
#import <UIKit/UIKit.h>
#interface Cell : UIView
#property (nonatomic, strong) UIImageView *leftImage;
#property (nonatomic, strong) UILabel *textLabel;
#property (nonatomic, strong) UIImageView *rightImage;
#end
Cell Implementation File
#import "Cell.h"
#implementation Cell
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self initViews];
[self initConstraints];
}
return self;
}
-(void)initViews
{
self.backgroundColor = [UIColor grayColor];
self.leftImage = [[UIImageView alloc] init];
self.leftImage.contentMode = UIViewContentModeScaleAspectFill;
self.leftImage.clipsToBounds = YES;
self.leftImage.layer.cornerRadius = 10.0;
self.textLabel = [[UILabel alloc] init];
self.textLabel.numberOfLines = 0;
self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.rightImage = [[UIImageView alloc] init];
self.rightImage.contentMode = UIViewContentModeScaleAspectFill;
self.rightImage.layer.cornerRadius = 10.0;
self.rightImage.clipsToBounds = YES;
[self addSubview:self.leftImage];
[self addSubview:self.textLabel];
[self addSubview:self.rightImage];
}
-(void)initConstraints
{
self.leftImage.translatesAutoresizingMaskIntoConstraints = NO;
self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.rightImage.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"leftImage": self.leftImage,
#"textLabel": self.textLabel,
#"rightImage": self.rightImage
};
// horizontal constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[leftImage(20)]-10-[textLabel]-[rightImage(20)]-10-|" options:0 metrics:nil views:views]];
// vertical constraints
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.leftImage attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraint:[NSLayoutConstraint constraintWithItem:self.rightImage attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[leftImage(20)]" options:0 metrics:nil views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[rightImage(20)]" options:0 metrics:nil views:views]];
}
#end
You should see something like this:
Maybe you have your reason's for using a scrollView and manually adding each row, otherwise, if you're open to alternative, you can use UICollectionView or UITableView.
The method above will result in a lot of memory use as you can imagine if you had 1000 rows, the app needs to calculate, rendering and store 1000 rows in memory. Not scalable, and not feasible.
That's where UITableView or UICollectionView comes in, they reuse each cell when it goes offscreen that way, you'll only ever need to render and store the visible cells on screen.
UICollectionView Demo
So, if you want to see a UICollectionView approach, this is a demo of how you can do it:
ViewController Header File
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
#property (nonatomic, strong) UICollectionView *collectionView;
#property (nonatomic, strong) NSArray *items;
#end
ViewController Implementation File
#import "ViewController.h"
#import "CustomCell.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initViews];
[self initConstraints];
// --------------------------------------------------------
// Hardcoding 15 sample items as the data source.
// Your data might be from a JSON webservice REST API.
// --------------------------------------------------------
self.items = #[
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer.",
#"This is my example text, it could be longer."
];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)initViews
{
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.minimumInteritemSpacing = 0;
flowLayout.minimumLineSpacing = 10;
self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
self.collectionView.backgroundColor = [UIColor whiteColor];
// need to tell CollectionView beforehand the cell class you want to use
[self.collectionView registerClass:[CustomCell class] forCellWithReuseIdentifier:#"cellID"];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
[self.view addSubview:self.collectionView];
}
-(void)initConstraints
{
self.collectionView.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"collectionView": self.collectionView
};
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[collectionView]|" options:0 metrics:nil views:views]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[collectionView]|" options:0 metrics:nil views:views]];
}
#pragma mark - UICollectionView Methods -
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.items.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
// note: reuse identifier must match what you specified in the register cell above
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cellID" forIndexPath:indexPath];
// ---------------------------------------------------------------
// hardcoding images here, you might load your images from JSON
// data using an image caching library like SDWebImage
// ---------------------------------------------------------------
cell.leftImage.image = [UIImage imageNamed:#"leftImage.png"];
// getting text data from data source "self.items"
cell.textLabel.text = self.items[indexPath.row];
cell.rightImage.image = [UIImage imageNamed:#"rightImage.png"];
return cell;
}
// ----------------------------------------------------------------
// Tells the collection view the width and height of each cell
// ----------------------------------------------------------------
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize size = CGSizeMake(self.view.frame.size.width, 50.0);
return size;
}
#end
CustomCell Header File
#import <UIKit/UIKit.h>
#interface CustomCell : UICollectionViewCell
#property (nonatomic, strong) UIImageView *leftImage;
#property (nonatomic, strong) UILabel *textLabel;
#property (nonatomic, strong) UIImageView *rightImage;
#end
CustomCell Implementation File
#import "CustomCell.h"
#implementation CustomCell
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
[self initViews];
[self initConstraints];
}
return self;
}
-(void)initViews
{
self.backgroundColor = [UIColor grayColor];
self.leftImage = [[UIImageView alloc] init];
self.leftImage.contentMode = UIViewContentModeScaleAspectFill;
self.leftImage.clipsToBounds = YES;
self.leftImage.layer.cornerRadius = 10.0;
self.textLabel = [[UILabel alloc] init];
self.textLabel.numberOfLines = 0;
self.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.rightImage = [[UIImageView alloc] init];
self.rightImage.contentMode = UIViewContentModeScaleAspectFill;
self.rightImage.layer.cornerRadius = 10.0;
self.rightImage.clipsToBounds = YES;
[self.contentView addSubview:self.leftImage];
[self.contentView addSubview:self.textLabel];
[self.contentView addSubview:self.rightImage];
}
-(void)initConstraints
{
self.leftImage.translatesAutoresizingMaskIntoConstraints = NO;
self.textLabel.translatesAutoresizingMaskIntoConstraints = NO;
self.rightImage.translatesAutoresizingMaskIntoConstraints = NO;
id views = #{
#"leftImage": self.leftImage,
#"textLabel": self.textLabel,
#"rightImage": self.rightImage
};
// horizontal constraints
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-10-[leftImage(20)]-10-[textLabel]-[rightImage(20)]-10-|" options:0 metrics:nil views:views]];
// vertical constraints
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.leftImage attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.textLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.rightImage attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[leftImage(20)]" options:0 metrics:nil views:views]];
[self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[rightImage(20)]" options:0 metrics:nil views:views]];
}
#end
You end up with something like this:
Look the same but more efficient :D
No I didn't upload the same screenshot :P
Hope that helps?

Resources