Easier way to change all the buttons? - iphone

I have 10 UIButtons that I want to change the background color of.
Here's what I have right now:
b1.backgroundColor = [UIColor redColor];
b2.backgroundColor = [UIColor redColor];
b3.backgroundColor = [UIColor redColor];
b4.backgroundColor = [UIColor redColor];
b5.backgroundColor = [UIColor redColor];
b6.backgroundColor = [UIColor redColor];
b7.backgroundColor = [UIColor redColor];
b8.backgroundColor = [UIColor redColor];
b9.backgroundColor = [UIColor redColor];
b10.backgroundColor = [UIColor redColor];
I wonder if there is another, simpler, way of doing this. I have all ready tried UIButton.backgroundColor = [UIColor redColor] but that didn't work.

//Make an array of the buttons:
NSArray* buttons=[[NSArray alloc] initWithObjects:b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,nil];
//Loop through them
for(UIButton* b in buttons)
{
b.backgroundColor = [UIColor redColor];
}
The array could also be initialized in viewDidLoad.

Put the buttons in an array:
NSArray* buttonArray=[[NSArray alloc] initWithObjects:b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,nil];
Then set the background color for all the buttons:
[buttonArray makeObjectsPerformSelector:#selector(setBackgroundColor:) withObject:[UIColor redColor]];

Create an array of your buttons in viewDidLoad. Then just use a for loop to change colors.

Related

Can't set custom BackgroundColor on UITableViewCell

I'm using this code to customize the colour of my cell's background view:
UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0 green:255.0 blue:255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;
The thing is, it doesn't work. This code though, works perfectly fine:
cell.contentView.backgroundColor = [UIColor redColor];
What's the problem here? Can't I customize the colour of a cell to my liking? Or am I doing something wrong?
you have to set Cell Background with this Delegate:-
- (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
forRowAtIndexPath: (NSIndexPath*)indexPath
{
// UIImage *img = [UIImage imageNamed:#"button.png"]; // get your background image
UIColor *cellBackgroundColor = [UIColor colorWithRed:255/255.0 green:108/255.0 blue:61/255.0 alpha:1.0]; //if you want to set color then use this line
// UIColor *backgroundColor = [[UIColor alloc] initWithPatternImage: img];
cell.backgroundColor = cellBackgroundColor;
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
}
Your table look like:-
Taking an RGB color and normalizing it with UIColor on the iPhone
Your values are between 0 and 255. Use them to create a UIColor:
float r; float g; float b; float a;
[UIColor colorWithRed:r/255.f
green:g/255.f
blue:b/255.f
alpha:a/255.f];
Just change :
UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
cell.contentView.backgroundColor = cellBackgroundColor;
cell.backgroundView.backgroundColor
you can make a custom view and can change the complete backgroundview
UIView *backgroundViewForCell = [[[UIView alloc] init] autorelease];
backgroundViewForCell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"row-bg.png"]];
UIView *selectedBackgroundViewForCell = [[[UIView alloc] init] autorelease];
selectedBackgroundViewForCell.backgroundColor = [[UIColor blueColor] colorWithAlphaComponent:.2];
cell.backgroundView = backgroundViewForCell;
cell.selectedBackgroundView = selectedBackgroundViewForCell;
Maybe you can add cell.contentView.backgroundColor = [UIColor clearColor] before you set your color.
The reason is that the backgroundview is at the bottom. The contentview is at the top.
Other LOgic IS
UIView *myBackView = [[UIView alloc] initWithFrame:cell.frame];
myBackView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:0.75 alpha:1];
[cell.contentView addSubview:myBackView];
[myBackView release];
you are on the right track. You just need to replace your color code with this
UIColor *cellBackgroundColor = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0];
Enjoy Programming!!

Changing UITableView to clear color

I want my UITableView to be completely invisible but I have set a lot of things and nothing seems to be working.
I use custom table cells.
This is what I set in the cell.h
self.textLabel.backgroundColor = [UIColor clearColor];
primaryLabel.backgroundColor = [UIColor clearColor];
secondaryLabel.backgroundColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor clearColor];
self.backgroundView.backgroundColor = [UIColor clearColor];
self.accessoryView.backgroundColor = [UIColor clearColor];
self.selectedBackgroundView.backgroundColor = [UIColor clearColor];
And I also set
_tableView.backgroundColor = [UIColor clearColor];
But still it is not working, what am I missing?
Instead, you could use the hidden property of the tableView.
tableView.hidden = YES;
With regards to the "incorrect" answer given, I think you mean is that you want the tableView background to disapear (showing the views background).
If so, in your viewDidLoad, add the following: (given you made your tableView programmatically)
[self.tableView setBackgroundColor:[UIColor clearColor]];
If you also don't want to show the lines for each cell:
[self.tableView setSeparatorColor:[UIColor clearColor]];

iPhone Background Image for Table Cell

How can I make the background of the accessory clear? I've managed to set the rest of the elements to [UIColor clearcolor] but it doesn't work for the accessory...
Here's what my progress looks like:
[self.accessoryView setOpaque: NO];
self.accessoryView.backgroundColor = [UIColor clearColor];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Icon.png"]];
self.accessoryView.backgroundColor = [UIColor clearColor];
Thanks for any help you guys can offer!
have you tried
cell.accessoryView.backgroundColor = [UIColor clearColor];

Create alpha UIView with an UIlabel in front of it

i would like make an UIView, with an alpha and with a label.
but i want the UILabel in front of all like this:
How to do it?
Here the code:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 200, 20)];
lbl.text = #"AAAAAA";
lbl.backgroundColor = [UIColor clearColor];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(50, 210, 230, 50)];
v.backgroundColor = [UIColor greenColor];
v.alpha = 0.5;
[v addSubview:lbl];
[self.view addSubview:v];
}
The green view is with alpha 0.5... like the text and this is wrong!!!
thanks.
Instead of setting the alpha of the whole view, just set the background color to a color with transparency.
So change this:
v.backgroundColor = [UIColor greenColor];
v.alpha = 0.5;
To this:
v.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:0.5];
If you want to use the built in colors, see colorWithAlphaComponent:
accessoryView.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
In the XCode 4 Interface Builder you can achieve this by clicking on the Background property of a view, clicking on "Other", and choosing the color and the opacity values in the color picker that appears.

How do I get the UIToolbar font to match UINavigation Controller?

Using the following post, I was able to add a UILabel to a UIToolbar, however, it looks crappy. Anyone know how to get the text size / color / shadow to match the title for a UINavigationController?
Navigation Controller
alt text http://www.codingwithoutcomments.com/uploads/Picture1.png
UIToolbar with UILabel
alt text http://www.codingwithoutcomments.com/uploads/Picture2.png
What steps do I need to take to make them match?
You might also want to add a shadow?
itemLabel.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
itemLabel.shadowOffset = CGSizeMake(0, -1.0);
Obviously the question is about iPhone, but in case anyone is wondering, here are the numbers for iPad:
titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
titleLabel.shadowOffset = CGSizeMake(0, 1);
titleLabel.shadowColor = [UIColor colorWithWhite:1.f alpha:.5f];
titleLabel.textColor = [UIColor colorWithRed:113.f/255.f green:120.f/255.f blue:127.f/255.f alpha:1.f];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.text = #"Title";
UILabel * itemLabel = [[UILabel alloc] initWithFrame:rectArea];
itemLabel.text = #"Item";
itemLabel.textColor = [UIColor whiteColor];
itemLabel.font = [UIFont boldSystemFontOfSize:22.0];