UILabel/Content not appearing in UIScrollView - ios

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];
}

Related

How to have both UILabel and UIButton items into a UIView for UITableView header section

This is my first post here, so I hope to do everything correctly.
I'm trying to write a code in Objective-C that allows me to add a UIButton in a table header view, for a specific section.
This is how it should look:
I see correctly the title, but unfortunately the UIButton doesn't appear and I don't know why.. some months ago I've used a similar code and it worked, but after changing something due to another bug in the table view, it stopped working and unfortunately I haven't the original code anymore..
Do you know where I'm wrong or do you know any alternative?
This is the code in Objective-C (that should automatically align the title and the button, even when the user changes the device orientation and it should work for any screen size):
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 2) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 0,0)];
[view setClipsToBounds:NO];
[view setPreservesSuperviewLayoutMargins:TRUE];
UILabel *textLabel = [[UILabel alloc] init];
textLabel.text = #"MY TITLE";
textLabel.textColor = [UIColor colorWithRed:0.427f green:0.427f blue:0.427f alpha:1.0f];
//[textLabel setFont:_tableViewFooterFont];
[textLabel setTextAlignment:4]; // Natural
[textLabel setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[textLabel setAccessibilityTraits:UIAccessibilityTraitHeader];
[view addSubview:textLabel];
UIButton *button = [UIButton buttonWithType:1];
//[button setHidden:FALSE];
//[button.titleLabel setFont:_tableViewFooterFont];
[button.titleLabel setTextColor:[UIColor redColor]];
[button setTranslatesAutoresizingMaskIntoConstraints:FALSE];
[button setTitle:#"RESET" forState:0];
[button setContentCompressionResistancePriority:0 forAxis:UILayoutConstraintAxisHorizontal];
[button addTarget:self action:#selector(resetAction:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
NSDictionary *dictionary = NSDictionaryOfVariableBindings(textLabel, button);
NSLayoutConstraint *buttonConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"|-[textLabel]->=0-[button]-|" options:0 metrics:NULL views:dictionary];
[view addConstraints:buttonConstraint];
NSLayoutConstraint *labelConstraint = [NSLayoutConstraint constraintsWithVisualFormat:#"V:|-12-[textLabel]-6-|" options:0 metrics:NULL views:dictionary];
[view addConstraints:labelConstraint];
NSLayoutConstraint *viewConstraint = [NSLayoutConstraint constraintWithItem:button attribute:10 relatedBy:0 toItem:textLabel attribute:10 multiplier:1.0 constant:0];
[view addConstraint:viewConstraint];
return view;
}
return NULL;
}
Try this:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create view for header
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)]; // set header view height as you want
headerView.backgroundColor = [UIColor clearColor] ;
// create label for header title
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, tableView.frame.size.width-130, 30)];
title.font = [UIFont fontWithName:#"FONT_NAME" size:16.0]; //optional
[title.heightAnchor constraintEqualToConstant:30].active = true;
[title.widthAnchor constraintEqualToConstant:tableView.frame.size.width-130].active = true;
// create button for header action
UIButton *resetButton = [[UIButton alloc] initWithFrame:CGRectMake(self.yourTableView.frame.size.width-120, 0, 120, 30)];
[resetButton setTitle:#"RESET" forState:UIControlStateNormal];
//resetButton.backgroundColor = [UIColor redColor];
[resetButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[resetButton setFont:[UIFont fontWithName:#"FONT_NAME" size:16.0]] ;
[resetButton.heightAnchor constraintEqualToConstant:30].active = true;
[resetButton.widthAnchor constraintEqualToConstant:120].active = true;
//Stack View
UIStackView *stackView = [[UIStackView alloc] initWithFrame:headerView.frame];
stackView.axis = UILayoutConstraintAxisHorizontal;
stackView.distribution = UIStackViewDistributionFillProportionally;
stackView.alignment = UIStackViewAlignmentCenter;
stackView.spacing = 10;
[stackView addArrangedSubview:title];
[stackView addArrangedSubview:resetButton];
stackView.translatesAutoresizingMaskIntoConstraints = false;
[headerView addSubview:stackView];
if(section == 2){
title.text = #"MY TITLE";
[resetButton addTarget:self action:#selector(resetBtnActn:) forControlEvents:UIControlEventTouchUpInside] ;
}else{
headerView.hidden = YES ;
title.hidden = YES ;
resetButton.hidden = YES ;
}
//Trailing
NSLayoutConstraint *trailing =[NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeTrailing
multiplier:1.0f
constant:0.f];
//Leading
NSLayoutConstraint *leading = [NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeLeadingMargin
multiplier:1.0f
constant:0.f];
//Bottom
NSLayoutConstraint *bottom =[NSLayoutConstraint
constraintWithItem:stackView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:headerView
attribute:NSLayoutAttributeBottom
multiplier:1.0f
constant:0.f];
//Add constraints
[headerView addConstraint:trailing];
[headerView addConstraint:bottom];
[headerView addConstraint:leading];
// 5. Finally return
return headerView;
}
// table section button action
-(IBAction)resetBtnActn:(id)sender
{
NSLog(#"button tapped");
}
comment if you have any issue.

UIScrollView not working properly with layout constraints

Im trying to place 20Textfields vertically and adding constraints to them programmatically and here is my code for that.
//For creating 20 textfields
-(void)createTextFields
{
//create textfields using for loop
for (int i=0; i<20; i++) {
[self createNew:i+1];
}
}
-(void)createNew:(int )count
{
UITextField *textField=[[UITextField alloc]init];
textField.translatesAutoresizingMaskIntoConstraints=NO;
textField.placeholder=[NSString stringWithFormat:#"Enter text:%u",count];
textField.font=[textField.font fontWithSize:10];
textField.backgroundColor = [UIColor colorWithHue:0.8 saturation:0.1 brightness:0.9 alpha:1];
[containerView addSubview:textField];
[inputFieldsArr addObject:textField];
//Left and width
[containerView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"|-[textField]-|"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(textField)]];
//Top and height
[containerView addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-count-[textField(30)]"
options:0
metrics:#{#"count":[NSNumber numberWithInt:count*50]}
views:NSDictionaryOfVariableBindings(textField)]];
// return textField;
}
//Adding a containerview to scrollview
-(void)createScroll
{
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollview];
scrollview.backgroundColor=[UIColor blackColor];
[scrollview setContentSize:CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height)];
UIView *view=self.view;
//Left and width
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"|-[scrollview]-|"
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(scrollview)]];
//Top and height
[self.view addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|[scrollview]|"
options:0
metrics:nil
views:NSDictionaryOfVariableBindings(scrollview,view)]];
containerView = [[UIView alloc] init];
containerView.backgroundColor = [UIColor yellowColor]; // just so I can see it
containerView.translatesAutoresizingMaskIntoConstraints = NO;
[scrollview addSubview:containerView];
[self addCOnstriantsToContainer:0];
float height=self.view.frame.size.height;
// CGSize fittingSize = [scrollview systemLayoutSizeFittingSize: UILayoutFittingCompressedSize];
//
// NSLog( #"container fitting size: %#", NSStringFromCGSize( fittingSize ));
//Left and width
}
//Adding Constriants to containerview
-(void)addCOnstriantsToContainer:(float)height
{
float width=self.view.frame.size.width;
// NSLog(#"height:%f",containerView.bounds.size.height);
[scrollview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"|-[containerView(width)]-|"
options:0
metrics:#{#"width":[NSNumber numberWithFloat:width]}
views:NSDictionaryOfVariableBindings(containerView,scrollview)]];
//Top and height
[scrollview addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat:#"V:|-[containerView(==1000)]-|"
options:0
metrics:#{#"height":[NSNumber numberWithFloat:height]}
views:NSDictionaryOfVariableBindings(containerView,scrollview)]];
//constraintsWithVisualFormat:#"V:|-[containerView(1000)]-|"
}
If I use constraintsWithVisualFormat:#"V:|-[containerView(==1000)]-|", it works fine. But I don't want to assign values statically. How can I set them dynamically? I also have tried constraintsWithVisualFormat:#"V:|[containerView(==scrollView)]|". But none of them worked for me.They can't be scrolled beyond the height of the device.
Any ideas to fix this?
Try to add top and height constraints separately for container view, set height same as scrollView's height, and set priority of height constraint to Low Priority.

Auto Layout for UIScrollView w/ 2 Child Views

I have a scroll view that's set up inside a storyboard with auto layout constraints so that it fills the entire view. The scroll view contains two views managed by UIViewControllers instantiated from nibs. The code to include these subviews:
- (void)viewDidLoad {
[super viewDidLoad];
ContentViewController *contentVC = [ContentViewController new];
[[NSBundle mainBundle] loadNibNamed:#"ContentView" owner:contentVC options:nil];
self.scrollView.delegate = self;
[self addChildViewController:contentVC];
[self.scrollView addSubview:contentVC.contentTC.view];
[contentVC didMoveToParentViewController:self];
[self addChildViewController:contentVC.contentTC];
self.view.layer.borderColor = [[UIColor blackColor] CGColor];
self.view.layer.borderWidth = 5.0;
self.scrollView.layer.borderColor = [[UIColor blueColor] CGColor];
self.scrollView.layer.borderWidth = 5.0;
CGRect adminFrame = contentVC.view.frame;
adminFrame.origin.x = adminFrame.size.width;
ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:#"ShareView" bundle:nil];
[self addChildViewController:shareViewController];
[self.scrollView addSubview:shareViewController.view];
shareViewController.view.frame = adminFrame;
CGRect shareFrame = shareViewController.view.frame;
shareFrame.origin.x = shareFrame.size.width;
[self.view addSubview:self.scrollView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
// 4) Finally set the size of the scroll view that contains the frames
CGFloat scrollWidth = 2 * self.view.frame.size.width;
CGFloat scrollHeight = self.view.frame.size.height;
self.scrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;
Don't have the ability to post an image, but here's a link to what it looks like:
dropbox.com/s/e84ya3ffrdzytac/Screenshot%202015-08-19%2011.52.15.png
In the iphone 6+ simulator, the scrollview is smaller in both width and height than the window, so there's white space on the right and bottom.
In the iPhone 4S simulator, the width is correct, but the subviews are longer than the window, which means that they'll scroll down. This only works in the iPhone 5, even though all of the views are inferred and not set up for 4-inch screens.
Apologies if this is a redundant question, but I've been searching through SO and banging my head against the wall for two days now, help would be appreciated.
** EDIT: UPDATED CODE AND BEHAVIOR: **
- (void)viewDidLoad {
[super viewDidLoad];
ContentViewController *contentVC = [ContentViewController new];
[[NSBundle mainBundle] loadNibNamed:#"ContentView" owner:contentVC options:nil];
self.scrollView.delegate = self;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.scrollView];
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width * 2, self.view.frame.size.height)];
[self.scrollView addSubview:contentView];
[self addChildViewController:contentVC];
[contentView addSubview:contentVC.contentTC.view];
[contentVC didMoveToParentViewController:self];
[self addChildViewController:contentVC.contentTC];
CGRect adminFrame = contentVC.view.frame;
adminFrame.origin.x = adminFrame.size.width;
ShareViewController *shareViewController = [[ShareViewController alloc] initWithNibName:#"ShareView" bundle:nil];
[self addChildViewController:shareViewController];
[contentView addSubview:shareViewController.view];
shareViewController.view.frame = adminFrame;
self.view.layer.borderColor = [[UIColor blackColor] CGColor];
self.view.layer.borderWidth = 3;
self.scrollView.layer.borderColor = [[UIColor blueColor] CGColor];
self.scrollView.layer.borderWidth = 5;
contentView.layer.borderColor = [[UIColor greenColor] CGColor];
contentView.layer.borderWidth = 7;
CGRect shareFrame = shareViewController.view.frame;
shareFrame.origin.x = shareFrame.size.width;
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[v]|" options:0 metrics:nil views:#{#"v" : contentView}]];
[self.scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[v]|" options:0 metrics:nil views:#{#"v" : contentView}]];
Results: dropbox.com/s/29zwwn6inmdvz0e/Screenshot%202015-08-19%2014.27.47.png?dl=0 , dropbox.com/s/rqsdk9yubq84ph6/Screenshot%202015-08-19%2014.28.14.png?dl=0
Here's an image of the constraints from the storyboard: dropbox.com/s/8c5wkfw81sqd8ij/Screenshot%202015-08-19%2014.28.47.png?dl=0 (pinning edges to superview)
bound your scrollview from left,right,top and bottom then it look fine.
and it will look like..
here is code that i written with yours
(void)viewDidLoad
{
[super viewDidLoad];
UIViewController *contentVC = [[UIViewController alloc]init];
contentVC.view.backgroundColor = [UIColor greenColor];
self.scrollView.delegate = self;
[self addChildViewController:contentVC];
[self.scrollView addSubview:contentVC.view];
[contentVC didMoveToParentViewController:self];
[self addChildViewController:contentVC];
self.view.layer.borderColor = [[UIColor blackColor] CGColor];
self.view.layer.borderWidth = 5.0;
self.scrollView.layer.borderColor = [[UIColor blueColor] CGColor];
self.scrollView.layer.borderWidth = 5.0;
CGRect adminFrame = contentVC.view.frame;
adminFrame.origin.x = adminFrame.size.width;
UIViewController *shareViewController = [[UIViewController alloc] init];
shareViewController.view.backgroundColor = [UIColor grayColor];
[self addChildViewController:shareViewController];
[self.scrollView addSubview:shareViewController.view];
shareViewController.view.frame = adminFrame;
CGRect shareFrame = shareViewController.view.frame;
shareFrame.origin.x = shareFrame.size.width;
[self.view addSubview:self.scrollView];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
attribute:NSLayoutAttributeRight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeRight
multiplier:1.0
constant:0]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.scrollView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0]];
// 4) Finally set the size of the scroll view that contains the frames
CGFloat scrollWidth = 2 * self.view.frame.size.width;
CGFloat scrollHeight = self.view.frame.size.height;
self.scrollView.contentSize = CGSizeMake(scrollWidth, scrollHeight);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;
}

adding NSLayoutConstraint makes views disappear

I have a simple view (controller) with two subviews:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *viewA = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewA.backgroundColor = [UIColor redColor];
[self.view addSubview:viewA];
UIView *viewB = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
viewB.translatesAutoresizingMaskIntoConstraints = NO;
viewB.backgroundColor = [UIColor grayColor];
[self.view addSubview:viewB];
}
Of course they are overlapping at this point (i can only see viewB). So I add a constraint to make viewB go below viewA, like this:
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:viewB
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:viewA
attribute:NSLayoutAttributeBottom
multiplier:1.f
constant:0]];
This makes both views disappear. What am I doing wrong?
Thanks!
When using constraints, you should do everything with constraints (no setting of frames), so adding one constraint isn't enough to define you positions and sizes. I'm not sure why adding the one constraint negated the sizes you set for the views, but it seems that it does. You should do something like this to fully describe the views,
- (void)viewDidLoad {
[super viewDidLoad];
UIView *viewA = [UIView new];
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewA.backgroundColor = [UIColor redColor];
[self.view addSubview:viewA];
UIView *viewB = [UIView new];
viewB.translatesAutoresizingMaskIntoConstraints = NO;
viewB.backgroundColor = [UIColor grayColor];
[self.view addSubview:viewB];
NSDictionary *dict = NSDictionaryOfVariableBindings(viewA,viewB);
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[viewA(==50)]" options:0 metrics:nil views:dict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"[viewB(==50)]" options:0 metrics:nil views:dict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[viewA(==50)][viewB(==50)]" options:NSLayoutFormatAlignAllLeft metrics:nil views:dict]];
}

Creating a UICollectionViewCell with UIImage and UILabels creates slow scrolling

I create a subview with a UIview (acts as header), a UIImage, and 10 UILabels. I'm putting these into a UICollectionView as cells.
When designed completely, it does not scroll smoothly. If i remove all the UILabels, it scrolls smoothly.
I'm assuming it's sluggish cause the UICollectionView loads on demand, so when it needs each new cell, it has to draw it which locks up the main thread.
Is it just a matter that its too much for iOS to handle to create them? If so, is there another way I can put text into it?
what my cell looks like:
Here is DatasetFilterListPanelView, this creates the UIView that I put into the UICollectionViewCell. I did it this way cause I created this before I decided to use UICollectionView.
#implementation DatasetFilterListPanelView
-(id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.translatesAutoresizingMaskIntoConstraints = FALSE;
UIView *contentView = [self createContentView];
[self addSubview:contentView];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[contentView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(contentView)]];
}
return self;
}
-(UIView *) createContentView {
UIView *contentView = [[UIView alloc] initWithFrame:self.frame];
// contentView.translatesAutoresizingMaskIntoConstraints = FALSE;
contentView.backgroundColor = [UIColor myDarkGrayColor];
UIView *headerView = [self createHeaderView];
[contentView addSubview:headerView];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|[headerView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[headerView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView)]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"gear12.png"]];
imageView.translatesAutoresizingMaskIntoConstraints = FALSE;
imageView.backgroundColor = [UIColor blueColor];
self.imageView = imageView;
[imageView addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:imageView attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];
[contentView addSubview:imageView];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[imageView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(imageView)]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[headerView]-[imageView]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(headerView, imageView)]];
UILabel *acresLabel = [self createLabelWithTitle:#"Label01:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]];
[contentView addSubview:acresLabel];
UILabel *addedLabel = [self createLabelWithTitle:#"Label02:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]];
[contentView addSubview:addedLabel];
UILabel *typeLabel = [self createLabelWithTitle:#"Label03:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]];
[contentView addSubview:typeLabel];
UILabel *zonesLabel = [self createLabelWithTitle:#"Label04:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]];
[contentView addSubview:zonesLabel];
UILabel *sceneLabel = [self createLabelWithTitle:#"Label05:" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:12]];
[contentView addSubview:sceneLabel];
UILabel *acresValueLabel = [self createLabelWithTitle:#"Data" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]];
acresValueLabel.textAlignment = NSTextAlignmentLeft;
[contentView addSubview:acresValueLabel];
UILabel *addedValueLabel = [self createLabelWithTitle:#"Data" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]];
addedValueLabel.textAlignment = NSTextAlignmentLeft;
[contentView addSubview:addedValueLabel];
UILabel *typeValueLabel = [self createLabelWithTitle:#"Name" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]];
typeValueLabel.textAlignment = NSTextAlignmentLeft;
[contentView addSubview:typeValueLabel];
UILabel *zonesValueLabel = [self createLabelWithTitle:#"Data" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]];
zonesValueLabel.textAlignment = NSTextAlignmentLeft;
[contentView addSubview:zonesValueLabel];
UILabel *sceneValueLabel = [self createLabelWithTitle:#"Name" andFont:[UIFont fontWithName:HELVETICA_FONT_STYLE size:12]];
sceneValueLabel.textAlignment = NSTextAlignmentLeft;
[contentView addSubview:sceneValueLabel];
NSDictionary *views = NSDictionaryOfVariableBindings(headerView, imageView, acresLabel, acresValueLabel, addedLabel, addedValueLabel, typeLabel, typeValueLabel, zonesLabel, zonesValueLabel, sceneLabel, sceneValueLabel);
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[headerView]-[acresLabel]"
options:0
metrics:nil
views:views]] ;
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[acresLabel]-[addedLabel(==acresLabel)]-[typeLabel(==acresLabel)]-[zonesLabel(==acresLabel)]-[sceneLabel(==acresLabel)]-|"
options:NSLayoutFormatAlignAllRight
metrics:0
views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[acresValueLabel]-[addedValueLabel(==acresLabel)]-[typeValueLabel(==acresLabel)]-[zonesValueLabel(==acresLabel)]-[sceneValueLabel(==acresLabel)]-|"
options:NSLayoutFormatAlignAllLeft
metrics:nil
views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:[imageView]-20-[acresLabel]-[acresValueLabel]" options:0 metrics:nil views:views]];
return contentView;
}
-(UIView *)createHeaderView {
UIView *view = [UIView new];
view.translatesAutoresizingMaskIntoConstraints = FALSE;
view.backgroundColor = [UIColor blueColor];
view.clipsToBounds = YES;
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:[view(30)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
UILabel *title = [UILabel new];
title.translatesAutoresizingMaskIntoConstraints = FALSE;
title.text = #"Default text";
title.font = [UIFont fontWithName:HELVETICA_FONT_STYLE_BOLD size:14];
title.textColor = [UIColor whiteColor];
title.backgroundColor = [UIColor clearColor];
self.headerLabel = title;
[view addSubview:title];
[view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"H:|-[title]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(title)]];
[view addConstraint:[NSLayoutConstraint constraintWithItem:title attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];
self.headerGradient = [UIColor grayGradient];
self.headerGradient.frame = CGRectMake(0, 0, 360, 30);
[view.layer insertSublayer:self.headerGradient atIndex:0];
return view;
}
-(UILabel *)createLabelWithTitle:(NSString *)title andFont:(UIFont *)font; {
UILabel *label = [UILabel new];
label.translatesAutoresizingMaskIntoConstraints = FALSE;
label.text = title;
label.font = font;
label.textAlignment = NSTextAlignmentRight;
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
return label;
}
Here is my UICollectionViewCell file, i just addSubview a DatasetFilterListPanelView to it.
#implementation DatasetViewCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self addSubview:[[DatasetFilterListPanelView alloc] initWithFrame:CGRectMake(0, 0, 360, 160)]];
}
return self;
}
When I use the same panels in a UIScrollview, once they are all loaded and positioned, it will scroll smoothly. So it has to be the loading a cell on demand aspect of the UICollectionView.
I followed this UICollectionView Tutorial
EDIT: creating the cell:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
DatasetViewCell *datasetCell = [collectionView dequeueReusableCellWithReuseIdentifier:DatasetCellIdentifier forIndexPath:indexPath];
return datasetCell;
}
EDIT 2: Instrument tracing:
Ok, after much playing around I figured out the culprit: constraints! CodaFI was right. I didn't have that many constraints in the panel so i didn't think it could be the issue.
I created a nib file and removed autolayout and it now scrolls smoothly.
Lesson of the day: Constraints are slow to compute!
Generally the problem is that you don't reuse the cells. Make sure you use dequeueReusableCellWithReuseIdentifier:forIndexPath: to reuse existing cells.

Resources