Custom UIView in Custom UITableViewCell designed in Storyboard - ios

I am trying to implement some drawing in custom UITableViewCells. To do that, I have added custom UIView subclass to my cell and implemented the drawing in drawRect method of the custom UIView subclass.
Here's my approach:
I have a UITableView with custom UITableViewCells.
I have subclassed UITableViewCell but the design is implemented directly in my storyboard. The contents of the cell are standard elements (UILabels & UIImageViews) and one custom uiview subclass.
I have set up everything in storyboard using constraints & autolayout.
The custom uiview subclass contains drawing code inside the drawRect method.
The drawing will have to be customized based on some parameters for each UITableViewCell.
Question is, what is the proper way to customize the UIView subclass.
At the moment all I get is a black cube, and I cannot see any drawing... The background color doesn't change and the text does not appear (draw).
EDIT:
In cellForRowAtIndexPath I create the custom UIView using its initWithText: andColor: method:
- (instancetype)initWithText:(NSString *)text andColor:(UIColor *)color
{
NSParameterAssert(text);
self = [super init];
if (self)
{
self.text = text;
[self setBackgroundColor:color];
}
return self;
}
-(void)setBackgroundColor:(UIColor *)backgroundColor
{
self.circleColor = backgroundColor;
[super setBackgroundColor:[UIColor clearColor]];
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
[self.circleColor setFill];
CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect),
CGRectGetWidth(rect)/2, 0, 2*M_PI, YES);
CGContextFillPath(context);
}
- (void)drawSubtractedText:(NSString *)text inRect:(CGRect)rect
inContext:(CGContextRef)context
{
CGContextSaveGState(context);
CGContextSetBlendMode(context, kCGBlendModeDestinationOut);
CGFloat pointSize = 8;
UIFont *font = [UIFont boldSystemFontOfSize:pointSize];
CGContextTranslateCTM(context, 0,
(CGRectGetMidY(rect) - (font.lineHeight/2)));
CGRect frame = CGRectMake(0, 0, CGRectGetWidth(rect), font.lineHeight);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.font = font;
label.text = text;
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor blueColor];
[label.layer drawInContext:context];
CGContextRestoreGState(context);
}

You would have given tags in your storyboard for the Label and Image. Use those tags and get the reference of subviews as below
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//some code
UILabel* cellDate = (UILabel *)[cellRef viewWithTag:1]
UIImageView* imageView = (UIImageView *)[cellRef viewWithTag:2]
//some code
}

What you are doing is wrong. You already added UIView reference in story board UITableviewCell. And again you are creating a object for the same custom UIView class in cellForRowIndexpath method. What you can do is, remove the UIView from storyboard and in cellforrowindexpath method like shown below.
CustomUIClass * c = [[CustomUIClass alloc] init];
c.frame = CGRectMake(X, Y, Width, height);
[cell.contentView addSubview:c];
Here add your own frame values to prepare CGRect.

Related

How to reflect properties of UILabel to another?

Im trying to customize UITableViewController dynamically. So i have changed many properties of cell.textLabel. Now i want to copy these properties to detailTextLabel and to one label i have created through code. How it can be done?
cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
This is my cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
cell.textLabel.text=[_names objectAtIndex:indexPath.row];
cell.textLabel.tag=indexPath.row;
cell.detailTextLabel.text=[_phones objectAtIndex:indexPath.row];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"arrow.png"] ];
[imageView setFrame:CGRectMake(380,10,30,50)];
[cell addSubview:imageView];
//customize the seperator
UIView* separatorLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1000, 1)];/// change size as you need.
separatorLineView.backgroundColor = [UIColor grayColor];// you can also put image here
[cell.contentView addSubview:separatorLineView];
cell.contentView.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
cell.textLabel.textColor=[UIColor whiteColor];
cell.textLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
cell.textLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
//here i want to copy the properties
return cell;
}
For Swift3
class MyLabel: UILabel {
override func draw(_ rect: CGRect) {
super.draw(rect)
backgroundColor = UIColor(red: 0, green: 0.188235, blue: 0.313725, alpha: 1)
textColor = UIColor.white
font = UIFont(name: "HelveticaNeue", size: 26)
autoresizingMask = .flexibleRightMargin
}
}
Create a subclass of UILabel in this way.
#import "MyLabel.h"
#implementation MyLabel
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
self.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
self.textColor=[UIColor whiteColor];
self.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
self.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}
#end
And now create an object of this MyLabel and your properties will be set automatically and also just assign this class to your label through storyboard to the label in your cell.
Subclassing is the best way for implementing reusable code.
Or else you can even create an extension of class or even an class method in some class which accepts the UILabel and sets the properties but this all are not the best practices. Another problem with extensions is the you can only use self but not super. This may create problems in future when you have to extend the properties.
I hope I am clear and helpful.
You can use this method to make all the labels of UITabelViewCell to same property
Here just loop through the subViews and check whether the subview is of UILabel, If it is of UILabel then set the property you want.
My Code :
- (void)formatTheLabelForCell:(UITableViewCell *)cell
{
for (UIView *view in cell.contentView.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
UILabel *lbl = (UILabel *)view;
lbl.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
lbl.textColor=[UIColor whiteColor];
lbl.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
lbl.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}
}
}
Instead of configuring cell in cellForRowAtIndexPath, it's better if you use custom cell. Add imageView & separatorLineView in your Storyboard/ Nib itself. This way all cells are generated with these default properties. Also if you need to configure something through code, you can code it in your CustomCell.m file like this:
class CustomCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.textLabel.backgroundColor = UIColor.redColor
//do other configurations
}
Edit: Downloading images from web may be the reason for taking long time loading cells. Try downloading images asynchronously. You can also use this library: SDWebImage
Note: I know you want it in Objective C, above code in Swift is just for illustration.
As for swift, you can do this, it will copy all the attributes you applied to textLabel to detailTextLabel.
cell.detailTextLabel.attributedText = cell.textLabel.attributedText
First of all I don't think that there is a function to copy specific properties of a any UIKit component in iOS SDK. Therefore for you will have to write a custom function for this. Additionally there are some issues with your "cellForRowAtIndexPath" as pointed out by others in comments.
There are different solutions to this.
Solution 1:
Write a function in your view controller which take two labels as parameters and copy your desired values.
-(void)copyPropertiesFrom:(UILabel*)label1 toLabel:(UILabel*)label2{
label2.backgroundColor = label1.backgroundColor;
label2.textColor = label1.textColor;
label2.font = label1.font;
label2.autoresizingMask = label1.autoresizingMask;
}
In cellForRowAtIndexPath where you want to copy do this
[self copyPropertiesFrom:cell.titleLabel toLabel:cell.detailTextLabel];
Solution 2(Recommended): This is best in my little experience because you can reuse it in other view controllers. There might be a better approach than this.
Create a category of UILabel. Check this link How do I create a category in Xcode 6 or higher? and also this https://code.tutsplus.com/tutorials/objective-c-categories--mobile-10648
Your function within category will look like this.
-(void)formatLabelToMyStyle{
self.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
self.textColor = [UIColor whiteColor];
self.font = [UIFont fontWithName:#"HelveticaNeue" size:26];
self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}
You will include header file of category and call this function in your cellForRowAtIndexPath like this
[cell.titleLabel formatLabelToMyStyle];
[cell.detailTextLabel formatLabelToMyStyle];
[cell.customTextLabel formatLabelToMyStyle];
And as for your cellForRowAtIndexPath, larme mentioned in comments "Don't add subview like that in cells because cells are reused" This will keep adding views to your cell hence causing memory issues, Specially when you have large number of cell which in your case is true.
You could use a Category for UILabel or use a subclass of UILabel that should share the same styling.
A Category for the UILabel could look like:
// UILabel+CustomStyle.h
#import <UIKit/UIKit.h>
#interface UILabel (CustomStyle)
-(void) applyCustomStyle;
#end
.m file:
// UILabel+CustomStyle.m
#import "UILabel+CustomStyle.h"
#implementation UILabel (CustomStyle)
-(void) applyCustomStyle {
self.backgroundColor = [UIColor colorWithRed: 0 green: 0.188235 blue: 0.313725 alpha: 1];
self.textColor = [UIColor whiteColor];
self.font = [UIFont fontWithName: #"HelveticaNeue" size: 26];
self.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
}
#end
Then you can apply the same styling by simply calling:
#import "UILabel+CustomStyle.h"
[label applyCustomStyle];
If you want to use same label configuration to many places in project. Just subclass as #NikhilManapure said.
OR
If you want to apply same properties to TableViewCell textLabel and detailTextLabel. You should subclass TableViewCell and override Label properties in drawrect method.
Objective-C
#import <UIKit/UIKit.h>
#interface PropertiesCell : UITableViewCell
#end
#import "PropertiesCell.h"
#implementation PropertiesCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
[self cellLabelConfigure:self.textLabel];
[self cellLabelConfigure:self.detailTextLabel];
}
- (void)cellLabelConfigure:(UILabel*) contentLabel {
contentLabel.backgroundColor = [UIColor colorWithRed:0 green:0.188235 blue:0.313725 alpha:1];
contentLabel.textColor=[UIColor whiteColor];
contentLabel.font=[UIFont fontWithName:#"HelveticaNeue" size:26];
contentLabel.autoresizingMask=UIViewAutoresizingFlexibleRightMargin;
}
#end
Swift
class PropertiesCell: UITableViewCell {
override func draw(_ rect: CGRect) {
super.draw(rect)
cellLabelsConfigure(contentLabel: self.textLabel)
cellLabelsConfigure(contentLabel: self.detailTextLabel)
}
func cellLabelsConfigure(contentLabel: UILabel?) {
contentLabel?.backgroundColor = UIColor(red: 0.0, green: 0.188, blue: 0.313, alpha: 1.0)
contentLabel?.textColor = UIColor.white
contentLabel?.font = UIFont(name: "HelveticaNeue", size: 26.0)
contentLabel?.autoresizingMask = UIViewAutoresizing.flexibleRightMargin
}
}
In storyboard change cell class name to PropertiesCell
Create an extension class and use this copy method to pass all the properties you want to the new label.
#implementation UILabel (Copy)
- (UILabel *)copyProperties {
UILabel *label = [UILabel new];
[self copyPropertiesWithLabel:label];
return label;
}
- (void)copyPropertiesWithLabel:(UILabel *)label {
label.backgroundColor = self.backgroundColor;
label.textColor = self.textColor;
label.font = self.font;
label.autoresizingMask = self.autoresizingMask;
// Add more properties
}
#end
Usage:
// cell.textLabel has now all the properties
[theLabelToBeCopied copyPropertiesWithLabel:cell.textLabel];
Swift3 version:
extension UILabel {
func copyProperties() -> UILabel {
var label = UILabel()
self.copyProperties(with: label)
return label
}
func copyProperties(with label: UILabel) {
label.backgroundColor = self.backgroundColor
label.textColor = self.textColor
label.font = self.font
label.autoresizingMask = self.autoresizingMask
// Add more properties
}
}
Usage:
theLabelToBeCopied.copyProperties(with: cell.textLabel)
you can do this for swift.it will copy for all attributes (textLabel to detailTextLabel).i think #Nikhil Manapure given exact answer.
cell.detailTextLabel.attributedText = cell.textLabel.attributedText

Dynamically change a cell's backgroundView.backgroundColor

I have the following code to create a cell's background view as a rounded rectangle:
- (UIView *)createBackgroundView
{
CGRect background = CGRectMake(50, 25, self.frame.size.width - 60, 90);
UIView *backgroundView = [[UIView alloc] initWithFrame:background];
backgroundView.backgroundColor = [UIColor colorWithRed:0.78 green:0.96 blue:0.39 alpha:1];
[backgroundView.layer setCornerRadius:7.0f];
[backgroundView.layer setMasksToBounds:YES];
return backgroundView;
}
Later on in the program I want to update the backgroundView's backgroundColor, I've tried
cell.backgroundView.backgroundColor = [UIColor redColor];
in both cellForRowAtIndexPath and willDisplayCell and neither seems to update it.
The way you have to go about it is:
[cell setBackgroundView:[self createBackgroundView]];
Probably you should also add an argument to the method:
- (UIView *)createBackgroundView:(UIColor *)color;

Objective C UItableView header custom view black

I am creating a custom view for my header and it only shows up as a black rectangle.
The code for the tableviewcontroller is:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
head* headerView = [[head alloc]init];
headerView.name=[sectionList objectAtIndex:section];
[headerView setNeedsDisplay];
return headerView;
}
where head is the custom view with a drawrect function:
- (void)drawRect:(CGRect)rect
{
// Drawing code
[self setBackgroundColor:[UIColor colorWithRed:125 green:125 blue:125 alpha:.5]];
bounds = [self bounds];
CGContextRef context = UIGraphicsGetCurrentContext();
CGSize titleSize=CGSizeFromString(self.name);
CGRect titleRect={CGPointMake(10, 10),titleSize};
UILabel *title = [[UILabel alloc] initWithFrame:titleRect];
[title setTextColor:[UIColor blackColor]];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[title setText:self.name];
[self addSubview:title];
CGContextSetRGBFillColor(context, 80, 168, 206, 0.5);
CGContextAddEllipseInRect(context, CGRectMake(titleRect.origin.x, 0,20, 20));
}
Also when I try to set the name in the viewController (see above). The name remains null.
When I tried to do it without a custom UIView. This time it only showed a white view:
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width,40.0f)];
[headerView setBackgroundColor:[UIColor colorWithRed:71 green:71 blue:71 alpha:.5]];
UIImage *reps = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: #"test" ofType: #"png"]];
CGSize titleSize=CGSizeFromString([sectionList objectAtIndex:section]);
CGRect titleRect={CGPointMake(0, 0),titleSize};
UILabel *groupTitle = [[UILabel alloc]initWithFrame:titleRect];
[groupTitle setText:[sectionList objectAtIndex:section]];
[headerView addSubview:groupTitle];
UIImageView* repsView = [[UIImageView alloc]initWithImage:reps];
[headerView addSubview:repsView];
return headerView;
}
Thanks
You should create headerView by providing frame, for example:
head* headerView = [[head alloc]initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 40.0f)];
After that make sure you implemented table view delegate for table header height:
-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40.0f;
}
You should consider create custom view without overriding drawRect: method because this is very CPU expensive.

UICollectionView with Custom UICollectionViewCell

I have a UICollectionView with a custom cell. I had this working completely till I tried to create the collection view programmatically rather than from the storyboard. I did this because I was not able to change the frame size of the collection view for different screen sizes. So I tried to create a collection view programmatically. My problem is my custom cells are not working.
For my custom cells I draw a circle in the view, user cornerradius on the view of the cell, have a UILabel and a UIImageView.
I draw the circle and the perform the cornerradius thing in the drawRect: of the custom cell. The rest i.e: put image in ImageView and text in UILabel, I do it in the collectionView datasource method.
The problem I am having is I don't get the cornerradius thing working although weirdly enough I can draw a circle on the view. second problem is I don't get the image in the image view and no text in the label
Here is my code. I create the collection view in ViewDidload: method.
[layout setSectionInset:UIEdgeInsetsMake(24, 10, 24, 10)];
[layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
[layout setMinimumLineSpacing:15];
[layout setMinimumInteritemSpacing:10];
self.collectionView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
self.collectionView.delegate=self;
self.collectionView.dataSource=self;
[self.collectionView registerClass:[customCell class] forCellWithReuseIdentifier:#"cell"];
self.collectionView.backgroundColor= [UIColor blackColor];
self.searchBar.delegate = self;
self.locations = [[NSArray alloc]init];
self.location = [[NSString alloc]init];
[self.view addSubview:self.collectionView];
Here is the drawRect: for my customCell.m
- (void)drawRect:(CGRect)rect
{
UIBezierPath *circularpath = [[UIBezierPath alloc]init];
CGRect Rect = CGRectMake(6, 20, 130, 130);//338
CGPoint mypoint = CGPointMake(Rect.origin.x + (Rect.size.width / 2), Rect.origin.y + (Rect.size.height / 2));
NSLog(#"Circle center point::%f, %f", mypoint.x, mypoint.y);
circularpath = [UIBezierPath bezierPathWithOvalInRect:Rect];
circularpath.lineWidth = 3.0;
[[UIColor whiteColor]setStroke];
UIImage *ori = [UIImage imageNamed:#"drogba.jpg"];
UIImage *image = [[UIImage alloc]initWithCGImage:ori.CGImage scale:1.45 orientation:UIImageOrientationUp];
[[UIColor colorWithPatternImage:image]setFill];
NSLog(#"Circular path:%#", circularpath);
//this works
[circularpath stroke];
//this does not
self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}
and here is my datasource method
customCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"cell" forIndexPath:indexPath];
UIImage *image = [[UIImage alloc]init];
if([self.allInfo count]>0){
image = [self getImageForLocation:[self.allInfo objectAtIndex:indexPath.item]];
NSDictionary *dict = [[NSDictionary alloc]initWithDictionary:[self.allInfo objectAtIndex:indexPath.item]];
NSDictionary *city = [dict objectForKey:#"city"];
NSString *name = [[NSString alloc]initWithString:[city objectForKey:#"name"]];
cell.locationLabel.text = name;
cell.imageView.image = [self getImageForSky:dict];
cell.viewForBaselineLayout.backgroundColor = [UIColor colorWithPatternImage:image];
cell.tempLabel.text = [self getTempForLocation:dict];
}
NSLog(#"image description:%f", image.size.height);
count =1;
return cell;
I don't think it is a problem with my data because all I changed is creating Collection view programmatically rather than using storyboard.
EDIT::
I had to alloc init the image view and the labels for the custom cell and then add them as subview. now 3 of my 4 problems are solved. I still cannot get the corner radius to work for me. I want a slightly curved border for my cell
So firstly, because I removed the cell from the storyboard, I had to add the views to the cell's view as subview. Secondly, to get the rounded corners, I had to also set masksToBounds to YES All this had to be done in the initWithFrame: method of the custom cell. Here the code snippet
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.imageView = [[UIImageView alloc]init];
self.imageView.frame = CGRectMake(26, 27, 90, 90);
self.tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(26, 125, 90, 21)];
self.locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(11, 156, 121, 21)];
self.tempLabel.textAlignment = NSTextAlignmentCenter;
self.tempLabel.textColor = [UIColor whiteColor];
self.locationLabel.textAlignment = NSTextAlignmentCenter;
self.locationLabel.textColor = [UIColor whiteColor];
[self.viewForBaselineLayout addSubview:self.imageView];
[self.viewForBaselineLayout addSubview:self.tempLabel];
[self.viewForBaselineLayout addSubview:self.locationLabel];
self.viewForBaselineLayout.layer.masksToBounds = YES;
self.viewForBaselineLayout.layer.cornerRadius = 8.0f;
}
return self;
}
You have init method in Your custom cell
- (id)initWithFrame:(CGRect)frame
change corner radius in this method.
P.S. Works for me.

Changing Only Part of a Custom UITableViewCell Selected Background Color

I have a custom cell that I am controlling the color of when it is selected, ie the example code below:
UIView *selectedBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 600, 600)];
[selectedBackground setBackgroundColor:[UIColor selectedCellColor]];
self.selectedBackgroundView = selectedBackground;
This works, however I would only like to have part of the cell change colors when selected. My custom cell is broken down into many different subviews, and I have it sectioned out where I would be able to define the specific view that I would like to change colors for.
How can I control the selectedBackgroundView, or use a different method, to have the background color change encompass a single subview in my cell?
Ya you are in the rite way,by subclassing the UITableView cell
hear is the sample code that you may find the answer for your question :)
//in subclassed cell class
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.frame = CGRectMake(0, 0, 334, 250);
UILabel *aLabel1= [[UILabel alloc]init];
UILabel *aLabel2 = [[UILabel alloc]init];
self.label1 = aLabel1;
self.label2 = aLabel2;
self.label1.text = #"Happy";
self.label2.text = #"coding";
UIImageView *bg1 = [[UIImageView alloc]init];
bg1.tag = 100;
UIImageView *bg2 = [[UIImageView alloc]init];
bg2.tag = 200;
[self addSubview:bg1]; // you must add your background views first
[self addSubview:bg2];
[self addSubview:label1];//then other views
[self addSubview:label2];
[aLabel1 release];
[aLabel2 release];
[bg1 release];
[bg2 release];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
// Configure the view for the selected state
// hear only you can manage your background views, simply i am adding 2 imageviews by setting different colors
[super setSelected:selected animated:animated];
self.backgroundColor = [UIColor greenColor];
if(selected)
{
self.label1.backgroundColor = [UIColor redColor];
self.label2.backgroundColor = [UIColor brownColor];
UIImageView *bg1 = (UIImageView *)[self viewWithTag:100];
bg1.frame = CGRectMake(0, 0,334/2, 250);
bg1.backgroundColor = [UIColor yellowColor];
}
else
{
self.label1.backgroundColor = [UIColor brownColor];
self.label2.backgroundColor = [UIColor redColor];
UIImageView *bg2 =(UIImageView *) [self viewWithTag:200];
bg2.frame = CGRectMake(35, 0, 334/2, 250);
bg2.backgroundColor = [UIColor lightGrayColor];
}
}
-(void)layoutSubviews
{
//i am setting the frame for each views that i hav added
[super layoutSubviews];
self.label1.frame = CGRectMake(10, 10, 60, 35);
self.label2.frame = CGRectMake(65, 10, 60, 35);
}
hope helps u :)
note: i am using "without ARC"

Resources