Custom drawing UICollectionViewCell subViews for performance optimization - ios

In my UICollectionViewCell subclass I have this code to layout the subViews I want:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, 10, 190, 40)];
_titleLabel.font = [UIFont boldFlatFontOfSize:22];
_titleLabel.backgroundColor = [UIColor clearColor];
_fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, 35, 190, 30)];
_fromLabel.font = [UIFont flatFontOfSize:14];
_fromLabel.backgroundColor = [UIColor clearColor];
UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
containerView.backgroundColor = [[UIColor colorWithPatternImage:[UIImage imageNamed:#"cream_pixels"]] colorWithAlphaComponent:0.8];
[self.contentView addSubview:containerView];
_iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[_iconView setCenter:containerView.center];
_iconView.backgroundColor = [UIColor whiteColor];
_iconView.contentMode = UIViewContentModeScaleAspectFit;
_iconView.image = [UIImage imageNamed:#"envelopeIcon.png"];
_iconView.backgroundColor = [UIColor clearColor];
[containerView addSubview:_iconView];
_dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, self.contentView.frame.size.height - 23, self.contentView.frame.size.width-97, 25)];
[_dateLabel setBackgroundColor:[UIColor clearColor]];
[_dateLabel setFont:[UIFont boldFlatFontOfSize:13]];
[_dateLabel setTextColor:[[UIColor wetAsphaltColor] colorWithAlphaComponent:0.6f]];
[_dateLabel setTextAlignment:NSTextAlignmentLeft];
[_dateLabel setLineBreakMode:NSLineBreakByCharWrapping];
[_dateLabel setNumberOfLines:0];
_typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.f, 0.f , 4, self.contentView.frame.size.height)];
_typeLabel.layer.cornerRadius = 0.f;
_typeLabel.backgroundColor = [UIColor belizeHoleColor];
[self.contentView addSubview:_titleLabel];
[self.contentView addSubview:_fromLabel];
[self.contentView addSubview:_typeLabel];
[self.contentView addSubview:_dateLabel];
self.backgroundColor = [[UIColor cloudsColor] colorWithAlphaComponent:1.f];
self.layer.cornerRadius = 3.2f;
self.layer.masksToBounds = NO;
self.layer.contentsScale = [UIScreen mainScreen].scale;
self.layer.borderColor = [[UIColor silverColor] colorWithAlphaComponent:0.2].CGColor;
self.layer.borderWidth = 0.5f;
self.layer.shadowOpacity = 0.35f;
self.layer.shadowRadius = 3.f;
self.layer.shadowOffset = CGSizeMake(0, 3.f);
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.shouldRasterize = YES;
[_titleLabel setText:#"Nuevo Mensaje"];
}
return self;
}
I understand that for performance, it's recommended that the subviews are added to the cell using custom drawing, and not by layering the contents of it.
How can I achieve this?

Related

Tableview not scrolling smooth with static data

I am using custom cell and headerView with static images and footerView but not scrolling as smoothly as it should scroll, it is getting stuck in middle and then scrolls........
Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerview = [[UIView alloc]init];
if (section == 2) {
headerview.frame = CGRectMake(0, 30, tableView.frame.size.width, 117);
Button = [[UIButton alloc]initWithFrame:CGRectMake(0, 18, headerview.frame.size.width, 100)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, tableView.frame.size.width, 17)];
label.text = #"Siddharth and hiren Like this";
label.font = [UIFont systemFontOfSize:10];
label.textColor = [UIColor blackColor];
[headerview addSubview:label];
}
else
{
headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 100);
Button = [[UIButton alloc]initWithFrame:headerview.frame];
}
// sheap top left and top right
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:headerview.bounds
byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
headerview.frame = headerview.bounds;
maskLayer.path = maskPath.CGPath;
headerview.layer.mask = maskLayer;
// shape for button bottom
headerview.backgroundColor = [UIColor whiteColor];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 0, headerview.frame.size.width, 1);
bottomBorder.backgroundColor = [UIColor colorWithRed:217/255.0f green:217/255.0f blue:217/255.0f alpha:1].CGColor;
bottomBorder.shadowColor = [[UIColor blackColor] CGColor];
bottomBorder.shadowOpacity = 0.9f;
bottomBorder.shadowRadius = 0.3f;
[headerview.layer addSublayer:bottomBorder];
headerview.layer.shouldRasterize = YES;
// Button Imageview
UIImageView *buttonimg = [[UIImageView alloc]initWithFrame:Button.bounds];
buttonimg.image = [UIImage imageNamed:[_SecImgArray objectAtIndex:section]];
buttonimg.layer.shouldRasterize = YES;
[Button addSubview:buttonimg];
// imageview overlay
UIView *overlay = [[UIView alloc]initWithFrame:Button.bounds];
overlay.alpha = 0.3;
overlay.backgroundColor = [UIColor blackColor];
overlay.layer.shouldRasterize = YES;
[buttonimg addSubview:overlay];
[Button addTarget:self action:#selector(Expand: and:) forControlEvents:UIControlEventTouchUpInside];
Button.tag = section;
if ([[AllNewsdic objectForKey:#"posts"] count] == 0) {
[Button setTitle:[[[DownloadData objectForKey:#"posts"]objectAtIndex:section]objectForKey:#"title"] forState:UIControlStateNormal];
}
else
{
[Button setTitle:[[[AllNewsdic objectForKey:#"posts"]objectAtIndex:section]objectForKey:#"title"] forState:UIControlStateNormal];
}
[Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
Button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
Button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
Button.titleLabel.numberOfLines = 0;
Button.titleLabel.font = [UIFont fontWithName:#"Avenir-Black" size:12];
Button.layer.shadowColor = [UIColor greenColor].CGColor;
Button.layer.shadowOpacity = 0.4;
Button.layer.shadowRadius = 1.0f;
Button.layer.cornerRadius = 7;
Button.layer.masksToBounds = YES;
Button.layer.shouldRasterize = YES;
[headerview addSubview:Button];
return headerview;}
Footer
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *fotter = [[UIView alloc]initWithFrame:CGRectMake(0, -10, tableView.frame.size.width, 30)];
fotter.backgroundColor = [UIColor whiteColor];
// corner redius
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:fotter.bounds
byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
fotter.frame = fotter.bounds;
maskLayer.path = maskPath.CGPath;
// maskLayer.backgroundColor = [UIColor colorWithRed:217/255.0f green:217/255.0f blue:217/255.0f alpha:1].CGColor;
fotter.layer.mask = maskLayer;
// Bottom layer
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 32, fotter.frame.size.width, 2);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[fotter.layer addSublayer:bottomBorder];
// top Layer
CALayer *topLayer = [CALayer layer];
topLayer.frame = CGRectMake(13, 0, fotter.frame.size.width - 26, 0.7);
topLayer.backgroundColor = [UIColor colorWithRed:197/255.0f green:187/255.0f blue:188/255.0f alpha:1].CGColor;
topLayer.shadowOpacity = 0.6f;
topLayer.shadowRadius = 0.5f;
[fotter.layer addSublayer:topLayer];
// Like bUtton
likebutton = [[UIButton alloc]initWithFrame:CGRectMake(0, -3, fotter.frame.size.width/2, fotter.frame.size.height + 3)];
likebutton.titleLabel.font = [UIFont systemFontOfSize:10];
likebutton.titleLabel.tintColor = [UIColor colorWithRed:197/255.0f green:187/255.0f blue:188/255.0f alpha:1];
[likebutton addTarget:self action:#selector(like:) forControlEvents:UIControlEventTouchUpInside];
[likebutton setTitle:#"1 likes" forState:UIControlStateNormal];
likebutton.tag = section;
like = [[UIImageView alloc]initWithFrame:CGRectMake(40 ,8, 15, 15)];
like.image = [UIImage imageNamed:#"SimpleLike.png"];
[likebutton addSubview:like];
[likebutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[fotter addSubview:likebutton];
// Follow Button
NSLog(#"%f",fotter.frame.size.width/2 + 1);
follow = [[UIButton alloc]initWithFrame:CGRectMake(fotter.frame.size.width/2 , -3, fotter.frame.size.width/2, fotter.frame.size.height + 3)];
follow.titleLabel.font = [UIFont systemFontOfSize:10];
follow.titleLabel.tintColor = [UIColor colorWithRed:197/255.0f green:187/255.0f blue:188/255.0f alpha:1];
[follow setTitle:#"Follow" forState:UIControlStateNormal];
followimage = [[UIImageView alloc]initWithFrame:CGRectMake(35 ,8, 17, 17)];
followimage.image = [UIImage imageNamed:#"Follow.png"];
[follow addSubview:followimage];
[follow setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
follow.tag = section;
[follow addTarget:self action:#selector(Follow:) forControlEvents:UIControlEventTouchUpInside];
[fotter addSubview:follow];
return fotter;}
Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AllNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"AllNews"];
cell.one.text = [one objectAtIndex:indexPath.section];
cell.two.text = [two objectAtIndex:indexPath.section];
cell.three.text = [three objectAtIndex:indexPath.section];
[cell.three sizeToFit];
return cell;}
how to set tableview scroll smooth and effective
how to set interface properties
tableview data is static
Use reusable UITableViewCell inside function viewForHeaderInSection and inside viewForFooterInSection
Do not create the new UIView and buttons for each function call.
Your table view scrolling is not smooth because the views inside viewForHeaderInSection and viewForFooterInSection methods are allocated multiple times. Why ? Because whenever you scroll up and down, both the methods are called as and when required.
Check whether header view is already allocated. If it is nil, then and then allocate it and return it. Otherwise, just return it. Same way in case of footer view.
Add comment in case of any doubt..

How can we insert multiple titles on UInavigationBar in ios

Hi i am beginner in iOS and in my project i need to insert "titles" at "left side center" and "right side center" on UInavigationBar as like below image please help me how should i do below my requirement
See below code and try :
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = [[UIScreen mainScreen] bounds];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, 44)];
lbl1.backgroundColor = [UIColor redColor];
lbl1.textAlignment = NSTextAlignmentCenter;
lbl1.text = #"Dashboard";
[self.navigationController.navigationBar addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, 44)];
lbl2.backgroundColor = [UIColor redColor];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[self.navigationController.navigationBar addSubview:lbl2];
}
You can customize the titleView of navigation bar. Read the official documentation here.
As per your question, I tried the below working code:-
-(UIView *)getTitleView
{
CGRect frame = [[UIScreen mainScreen] bounds];
UIView *viewTitle = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, self.navigationController.navigationBar.frame.size.height)];
UILabel *lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width/2, self.navigationController.navigationBar.frame.size.height)];
lbl1.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
lbl1.textAlignment = NSTextAlignmentCenter;
[lbl1 setTextColor:[UIColor whiteColor]];
lbl1.text = #"Dashboard";
[viewTitle addSubview:lbl1];
UILabel *lbl2 = [[UILabel alloc] initWithFrame:CGRectMake(frame.size.width/2, 0, frame.size.width/2, viewTitle.frame.size.height)];
lbl2.backgroundColor = [UIColor colorWithRed:130/255.0f green:13/255.0f blue:23/255.0f alpha:1.0f];
[lbl2 setTextColor:[UIColor whiteColor]];
lbl2.textAlignment = NSTextAlignmentCenter;
lbl2.text = #"Profile";
[viewTitle addSubview:lbl2];
return viewTitle;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.titleView = [self getTitleView];
}
I divide the code in separate method to follow modular approach. For any part of code, which is of separate task, write that piece of code in different method. This will lead to more readability.

Using CAShapeLayer fill color issue

I want to draw a circle with text inside.Unable to display text.Any help?
Below image is ref. for expected behaviour.
Below is the code for reference:
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
lblTitle.text = #"Me";
lblTitle.textColor = [UIColor blackColor];
lblTitle.font = [UIFont fontWithName:#"HelveticaNeueLight" size:10.0] ;
lblTitle.textAlignment = NSTextAlignmentCenter;
[view addSubview:lblTitle];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 40, 40)].CGPath;
circleLayer.fillColor = [UIColor clearColor].CGColor;
circleLayer.fillColor = [UIColor colorWithRed:56.0/255.0 green:212.0/255.0 blue:203.0/255.0 alpha:1.0f].CGColor;
circleLayer.strokeColor = [UIColor blackColor].CGColor;
circleLayer.lineWidth = 1;
[lblTitle.layer addSublayer:circleLayer];
You can setting cornerRadius of UILabel as #Sujay said:
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 40, 40)];
lblTitle.text = #"Me";
lblTitle.textColor = [UIColor blackColor];
lblTitle.layer.borderWidth = 1;
lblTitle.layer.borderColor = [UIColor blackColor].CGColor;
lblTitle.layer.cornerRadius = lblTitle.bounds.size.height / 2;
lblTitle.layer.masksToBounds = YES;
lblTitle.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:212.0/255.0 blue:203.0/255.0 alpha:1.0f];
lblTitle.font = [UIFont fontWithName:#"HelveticaNeueLight" size:10.0] ;
lblTitle.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblTitle];

UIView not visible in UITableView Header

Why won't my UIView (created below) show up in the UITableViewHeader that I try to add it to?
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
self.tableView.tableHeaderView = nil;
view.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = #"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
self.tableView.tableHeaderView = view;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
view.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = #"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
return view;//Please note the header will look the same for each section. You can add a switch or if/else to augment this
}
-As a side note, if you're planning on using the label here to set the text for the header, I suggest looking into the following methods:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

Auto position subviews of UINavigationBar's titleView

I have a custom titleView for displaying Navigation title and sub-title.
The title font is bigger than sub-title.
Here's what I have now:
CGRect headerTitleSubtitleFrame = CGRectMake(0, 0, 200, 44);
UIView* _headerTitleSubtitleView = [[[UILabel alloc] initWithFrame:headerTitleSubtitleFrame] autorelease];
_headerTitleSubtitleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
_headerTitleSubtitleView.backgroundColor = [UIColor clearColor];
_headerTitleSubtitleView.autoresizesSubviews = YES;
CGRect titleFrame = CGRectMake(0, 0, 200, 24);
UILabel *titleView = [[[UILabel alloc] initWithFrame:titleFrame] autorelease];
titleView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
titleView.backgroundColor = [UIColor clearColor];
titleView.font = [UIFont boldSystemFontOfSize:20];
titleView.textAlignment = UITextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];
titleView.shadowColor = [UIColor darkGrayColor];
titleView.shadowOffset = CGSizeMake(0, -1);
titleView.text = #"Title";
titleView.adjustsFontSizeToFitWidth = YES;
CGRect subtitleFrame = CGRectMake(0, 24, 200, 20);
UILabel *subtitleView = [[[UILabel alloc] initWithFrame:subtitleFrame] autorelease];
subtitleView.backgroundColor = [UIColor clearColor];
subtitleView.font = [UIFont boldSystemFontOfSize:13];
subtitleView.textAlignment = UITextAlignmentCenter;
subtitleView.textColor = [UIColor whiteColor];
subtitleView.shadowColor = [UIColor darkGrayColor];
subtitleView.shadowOffset = CGSizeMake(0, -1);
subtitleView.text = #"subtitle";
subtitleView.adjustsFontSizeToFitWidth = YES;
_headerTitleSubtitleView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[_headerTitleSubtitleView addSubview:titleView];
[_headerTitleSubtitleView addSubview:subtitleView];
self.navigationItem.titleView = _headerTitleSubtitleView;
I want to have the ability to remove subTitleView at some point in my code,
but that makes the titleView look out of center (vertically).
So my question is, how would you implement auto-positioning here?
should I use layoutSubviews ?
if so, what would be the proper way to override it?
I thought that when the container view has an autoresizingMask it will adjust it's size according to the "total" size of it's subviews (?).
Thanks
I was able to resolve this issue by creating a separate view class and
implement layoutSubviews.
Title label subview is always attached.
Subtitle label subview can be removed/added whenever need - which
will call layoutSubViews to make re-position the Title label view in center.
- (id)initWithFrame:(CGRect)frame
{
CGRect containerFrame = CGRectMake(0, 0, 200, 44);
self = [super initWithFrame:containerFrame];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.autoresizesSubviews = YES;
_title = [[UILabel alloc] initWithFrame:CGRectZero];
_title.backgroundColor = [UIColor clearColor];
_title.font = [UIFont boldSystemFontOfSize:20];
_title.textAlignment = UITextAlignmentCenter;
_title.textColor = [UIColor whiteColor];
_title.shadowColor = [UIColor darkGrayColor];
_title.shadowOffset = CGSizeMake(0, -1);
_title.adjustsFontSizeToFitWidth = YES;
_subTitle = [[UILabel alloc] initWithFrame:CGRectZero];
_subTitle.backgroundColor = [UIColor clearColor];
_subTitle.font = [UIFont boldSystemFontOfSize:13];
_subTitle.textAlignment = UITextAlignmentCenter;
_subTitle.textColor = [UIColor whiteColor];
_subTitle.shadowColor = [UIColor darkGrayColor];
_subTitle.shadowOffset = CGSizeMake(0, -1);
_subTitle.adjustsFontSizeToFitWidth = YES;
self.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);
[self addSubview:_title];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
if (([self.subviews count] > 1) && ([[self.subviews objectAtIndex:1] isKindOfClass:[UILabel class]]))
{
[[self.subviews objectAtIndex:0] setFrame:CGRectMake(0,0,200,24)];
[[self.subviews objectAtIndex:1] setFrame:CGRectMake(0,24,200,20)];
}
else
[[self.subviews objectAtIndex:0] setFrame:self.bounds];
}

Resources