Picture is worth more than a thousand words :)
Here is the code:
// Initialization code
UIView *myContentView = self.contentView;
UIImage *image = [UIImage imageNamed: #"bg_top.png"];
UIImageView *imageview = [[UIImageView alloc] initWithImage: image];
self.backgroundView = imageview;
[imageview release];
self.label = [self newLabelWithPrimaryColor:[UIColor blackColor] selectedColor:[UIColor redColor] fontSize:15.0 bold:YES];
self.label.textAlignment = UITextAlignmentLeft;
[myContentView addSubview:self.label];
[self.label release];
self.textField = [[UITextField alloc] init];
[self.textField setTextColor:[UIColor grayColor]];
self.textField.font = [UIFont systemFontOfSize:13.0];
//self.textField.secureTextEntry = YES;
[self.textField setKeyboardType:UIKeyboardTypeEmailAddress];
myContentView.backgroundColor = [UIColor clearColor];
[myContentView addSubview:self.textField];
[self.textField release];
The question is how to remove that ugly white background from labels and make them transparent.
Or alternatively:
self.label.backgroundColor = [UIColor clearColor];
Just to sum things up. Based upon all your responses, the best solution is:
[self.label setBackgroundColor:[UIColor clearColor]];
Try to set the Background color with:
[self.label setBackgroundColor: [UIColor colorWithWhite: 1.0f alpha: 0.0f]];
or set the Alpha value to 0
[self.label setAlpha: 0.0f];
Hope that does the trick.
Related
I want to give a background image to my UITableView, so I tried this method:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImageView *v = [[UIImageView alloc] initWithFrame:self.view.bounds];
[v setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:v];
[self.view sendSubviewToBack: v];
...
}
This works fine. However, after pushViewController, this method doesn't work anymore (why?). So I changed a method:
- (void)viewDidLoad
{
[super loadView];
// Do any additional setup after loading the view from its nib.
self.tableView.backgroundView = nil;
self.tableView.backgroundColor = [[UIColor alloc]initWithPatternImage:[UIImage imageNamed:#"Background.png"]];
...
}
This can work, but the result is not what I expected. Somehow the background picture changes its size (like a partial enlargement), and the background of cells is darker than the previous one.
Anyone knows the reason? I've been stuck with this problem for a long time.
Thanks a lot!
Use this:
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"TableBackground.jpg"]];
self.tableView.backgroundColor = background;
[background release];
Edit:
UIImage *myImage = [[UIImage alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:#"TableBackground.jpg"];
UIColor *background = [[UIColor alloc] initWithPatternImage:myImage];
self.tableView.backgroundColor = background;
[background release];
Try This Code i am sure it'll work
UIImage * targetImage = [UIImage imageNamed:#"coloured_flower.jpg"];
UIGraphicsBeginImageContextWithOptions(tableView.frame.size, NO, 0.f);
[targetImage drawInRect:CGRectMake(0.f, 0.f, tableView.frame.size.width, tableView.frame.size.height)];
UIImage * resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
tableView.backgroundColor = [UIColor colorWithPatternImage:resultImage];
UITableView *tblView=[[UITableView alloc] init];
[tblView setFrame:CGRectMake(0, 0, 320, 300)];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:tblView.frame];
[imgView setImage:[UIImage imageNamed:#"cinemax.jpg"]];
tblView.backgroundView=imgView;
[self.view addSubview:tblView];
I'm trying to add a UIToolbar that shows the user karma in a NavigationBar. The problem I have is that the colors are not exactly equals, as you can see at the photo.
So, any idea How could I resolve this? Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 45.00f)];
UILabel *karma = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 0.0f, 40.0f, 44.00f)];
karma.text = [[NSUserDefaults standardUserDefaults] stringForKey:#"karma"];
karma.font = [UIFont fontWithName:#"Helvetica" size:(14.0)];
karma.textColor = [UIColor colorWithRed:(255.0) green:(255.0) blue:(255.0) alpha:1];
karma.backgroundColor = [UIColor colorWithRed:(64/255.0) green:(64/255.0) blue:(64/255.0) alpha:0];
UIImage *image = [UIImage imageNamed:#"ic_people.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0.0f, 7.5f, 30.0f, 30.00f);
// Add Pin button.
[tools setBarStyle: UIBarStyleDefault];
[tools addSubview:karma];
[tools addSubview:imageView];
// Add toolbar to nav bar.
UIBarButtonItem *karmaNav = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = karmaNav;
...
Thank you.
Why not embed your karma label and image inside of a UIView instead of a UIToolbar. Then, you'd be able to set the background color of that UIView to clearColor.
Something like this:
UIView *tools = [[UIView alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 80.0f, 45.00f)];
tools.backgroundColor = [UIColor clearColor];
UILabel *karma = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 0.0f, 40.0f, 44.00f)];
karma.text = [[NSUserDefaults standardUserDefaults] stringForKey:#"karma"];
karma.font = [UIFont fontWithName:#"Helvetica" size:(14.0)];
karma.textColor = [UIColor colorWithRed:(255.0) green:(255.0) blue:(255.0) alpha:1];
karma.backgroundColor = [UIColor colorWithRed:(64/255.0) green:(64/255.0) blue:(64/255.0) alpha:0];
UIImage *image = [UIImage imageNamed:#"ic_people.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0.0f, 7.5f, 30.0f, 30.00f);
[tools addSubview:karma];
[tools addSubview:imageView];
UIBarButtonItem *karmaNav = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = karmaNav;
How do I make UINavigationBar title to be user editable, I've tried setting the titleView to be a textfield however it doesn't look the same.. It's missing that outline or drop shadow. I'm aiming for it to look like the default one.
This is what i'm implementing at the moment:
_titleField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 26)];
[_titleField setDelegate:self];
_titleField.text = _bugDoc.data.title;
_titleField.font = [UIFont boldSystemFontOfSize:20];
_titleField.textColor = [UIColor whiteColor];
_titleField.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = _titleField;
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadTitle];
}
- (void)loadTitle
{
txtField_navTitle = [[UITextField alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x,7,self.view.bounds.size.width,31)];
txtField_navTitle.delegate = self;
[txtField_navTitle setBackgroundColor:[UIColor clearColor]];
txtField_navTitle.textColor = [UIColor whiteColor];
txtField_navTitle.textAlignment = UITextAlignmentCenter;
txtField_navTitle.borderStyle = UITextBorderStyleNone;
txtField_navTitle.font = [UIFont boldSystemFontOfSize:20];
txtField_navTitle.autocorrectionType = UITextAutocorrectionTypeNo;
txtField_navTitle.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[self.navigationItem setTitleView:txtField_navTitle];
txtField_navTitle.layer.masksToBounds = NO;
txtField_navTitle.layer.shadowColor = [UIColor whiteColor].CGColor;
txtField_navTitle.layer.shadowOpacity = 0;
[txtField_navTitle release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
self.title = textField.text;
return YES;
}
Please dont forget to #import <QuartzCore/QuartzCore.h>
Try this way this works for me.
// Custom Navigation Title.
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 300, 40)];
tlabel.text=self.title;
tlabel.textAlignment=NSTextAlignmentCenter;
tlabel.font = [UIFont boldSystemFontOfSize:17.0];
tlabel.textColor=[UIColor colorWithRed:7.0/255.0 green:26.0/255.0 blue:66.0/255.0 alpha:1.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
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!!
I have a grouped UITableView. I am trying to make a custom UITableViewCell background:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.contentView.opaque = YES;
self.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rowbg#2x"]];
//Change Indicator
CGRect rect;
rect = CGRectMake(0, 0, 10, 50);
changeImageIndicator = [[UIImageView alloc] initWithFrame:rect];
[self.contentView addSubview: changeImageIndicator];
//Symbol
rect = CGRectMake(10, 10, 200, 20);
symbolLabel = [[UILabel alloc] initWithFrame:rect];
symbolLabel.textAlignment = UITextAlignmentLeft;
symbolLabel.font = [UIFont fontWithName:#"Arial" size:22];
symbolLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
symbolLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview: symbolLabel];
//Company
rect = CGRectMake(10, 30, 180, 20);
companyLabel = [[UILabel alloc] initWithFrame:rect];
companyLabel.textAlignment = UITextAlignmentLeft;
companyLabel.font = [UIFont fontWithName:#"Arial" size:13];
companyLabel.adjustsFontSizeToFitWidth = YES;
companyLabel.minimumFontSize = 10.0;
companyLabel.backgroundColor = [UIColor clearColor];
companyLabel.textColor = [UIColor colorWithRed:118.0/255.0 green:118.0/255.0 blue:118.0/255.0 alpha:1.0];
[self.contentView addSubview: companyLabel];
//Price
rect = CGRectMake(190, 10, 100, 20);
priceLabel = [[UILabel alloc] initWithFrame:rect];
priceLabel.textAlignment = UITextAlignmentRight;
priceLabel.font = [UIFont fontWithName:#"Arial" size:20];
priceLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
priceLabel.backgroundColor = [UIColor clearColor];
[self.contentView addSubview: priceLabel];
//Change
rect = CGRectMake(190, 30, 100, 20);
changeLabel = [[UILabel alloc] initWithFrame:rect];
changeLabel.textAlignment = UITextAlignmentRight;
changeLabel.font = [UIFont fontWithName:#"Arial" size:15];
changeLabel.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
changeLabel.adjustsFontSizeToFitWidth = YES;
changeLabel.backgroundColor = [UIColor clearColor];
changeLabel.minimumFontSize = 10.0; //adjust to preference obviously
[self.contentView addSubview: changeLabel];
}
return self;
}
The background color bleeds past the rounded corners. See image:
How can I make this not bleed?
This works for iOS 3.0 and later:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
What about self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; ?
This worked for me when nothing else did. Set the background color in IB to the grouped Table view color (default). Then in code, set the color to clearColor. I also have the cells marked opaque=NO and clearsContextBeforeDrawing=NO, but those settings alone didn't change anything until I added the clearColor by code – Bdebeez
self.contentView.superview.opaque = YES;
self.contentView.superview.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"rowbg#2x"]];
Have you tried setting the backgroundColor of the backgroundView rather than the contentView?
I had to create a rounded top, middle and button graphic image and set it to the background view of the cell depending on which row it is.
Set the cell background color in the tableView:cellForRowAtIndexPath:
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Gradient.png"]];