I have custom UITableViewCell and two buttons inside. When I select row, background for buttons change for the same color as background of selected row, and border for buttons disappears.
How Can I disable change border/background color for button when row is selected ?
Thanks for reply
1) Create a UIView and assign the desired background color.
2) Render the UIView to an UIImage
3) Set the backgroundImage of the button for the desired state.
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
colorView.backgroundColor = color;
UIGraphicsBeginImageContext(colorView.bounds.size);
[colorView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setBackgroundImage:colorImage forState: UIControlStateHighlighted];
I achieved this using a clear background view for my cells and disabling selection color of the row, but I needed it that way. Don't know if it suits your need, but you can try this:
UIView *selectedView = [[UIView alloc] initWithFrame:cell.bounds];
selectedView.backgroundColor = [UIColor clearColor];
cell.selectedBackgroundView = selectedView;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
Hope this helps.
Do checkout this thread as well : Why do all backgrounds disappear on UITableViewCell select?
Related
i have a problem in setting UITableViewCell background color. I wanna change it into my own color, so I use this code to change that background color :
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [ColorManager backgroundInput];
bg.layer.masksToBounds = YES;
[cell setSelectedBackgroundView:bg];
I implement this code in CellForRowAtIndexPath method.
The weird thing I have is, when i tap into a cell, the separator line color being highlight just like the image below. I just wanna make it still dark, anybody have idea?
Thank you
After setting up your own background view,which will have the line or not,
you can clear the default tableview color
tablename.separatorColor = [UIColor clearColor];
OR
If you don't want to remove the default line then you can give same color of background view to line color
//You can specify the RGB of background view
tablename.separatorColor = [UIColor colorWithRed:R green:G blue:B alpha:1.0f];
I think the best option is
[tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
Another option is to set the backgroundColor of the UITableViewCell to a clear color.
cell.backgroundColor = [UIColor clearColor];
try it
Objective-c
tablename.separatorColor = [UIColor blackColor];
Swift
tablename.separatorColor = UIColor.blackColor();
I have a cell that has some text in it and was trying to add an image to the cell.
This is what Ive tried:
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 30, 30, 30)];
imageView.backgroundColor = [UIColor clearColor];
[imageView.layer setCornerRadius:5.0f];
[imageView.layer setMasksToBounds:YES];
[imageView setImage:[UIImage imageNamed:#"test2.jpeg"]];
[cell.contentView addSubview:imageView];
cell.imageView.clipsToBounds = YES;
This is what it looks like right now:
Is there a way to avoid the image from overlapping the text in the cell? Thanks in advance!
tableviewcells have their own imageView that will appear to the left of the content or atleast to the left of the cells text.
You are creating an extra imageView that just happens to have the same name.
Just change the location
cell.imageView.center = CGPointMake(x,y);
Check out https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/TableView_iPhone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451-CH7-SW11 which has a nice example of how to use the built-in UITableViewCell image view.
Every UITableViewCell already has an UIImageView property 'imageView', but it's only shown if you set the imageView properties image property (cell.imageView.image = /* image code */;)
The best thing you can do is create a custom UITableViewCell. Or perhaps add another UILabel to the current cell and set your text in that instead of using the default UILabel.
I have added an accessoryView UIView in my UITableCell when cellForRowAtIndexPath is called.
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flechalinterna.png"]];
cell.accessoryView.backgroundColor=[UIColor clearColor];
But when the user selects row at index path, the accessory view is not highlighted. I tried to avoid it setting the background color to clearColor but it does not make any difference. Getting the following effect shown in this image:
Try to use this one
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"flechalinterna.png"] highlightedImage:[UIImage imageNamed:#"flechalinterna.png"]];
I've been trying to do this for a while now. I just want to remove the rounded corners you can see on this picture :
Does anyone know how to do it ?
[EDIT 1 : CELL BUILDING]
I am subclassing UITableViewCell to make my own layout. And then I'm adding a background to these cells like below :
UIImageView* imageView = [ [ UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
imageView.image = cellBackground;
cell.backgroundView = imageView;
I you need to edit the backgroundview of the corresponding cell. E.g.
UIView *bg = [[UIView alloc] initWithFrame:cell.frame];
bg.backgroundColor = [UIColor whiteColor]; // or any color
cell.backgroundView = bg;
[bg release];
The corners aren't rounded by default, just so you know.
Do you have a background image for the cell that makes the corners
seem rounded (most likely)?
Is each cell actually a section of the
tableview and there is no space between them?
Are you testing on a
jailbroken iPhone and you have some winterboard theme installed that
changes the style of tableviews?
Using iOS, how would I go about creating a red "delete" button similar to the one used when deleting contacts on the iPhone?
You first start with a stretchable image:
alt text http://grab.by/4lP
Then you make a button with the stretched image as the background and apply text.
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
Obviously, you will need to adjust the frame origin and size to match your app, as well as the target, selector, and title.
I've also made some buttons...retina and non-retina versions
If you want to use them in a Cell just use the following code in cellForRowAtIndexPath:
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:[cell.contentView frame]];
[sampleButton setFrame:CGRectMake(0, 0, cell.bounds.size.width-20, 44)];
[sampleButton setBackgroundImage:[UIImage imageNamed:#"button_red.png"] forState:UIControlStateNormal];
[cell addSubview:sampleButton];
I think those ones are better (and they look fine on retina display too) :
.png generated from this very nice .psd file : http://www.teehanlax.com/blog/2010/08/12/iphone-4-gui-psd-retina-display/
And then use it as a strechable image for the background of your UIButton:
UIImage* greenButton = [UIImage imageNamed:#"UIButton_green.png"];
UIImage *newImage = [greenButton stretchableImageWithLeftCapWidth:greenButton.size.width/2 topCapHeight:greenButton.size.height/2];
[callButton setBackgroundImage:newImage forState:UIControlStateNormal];
Probably the simplest way to do it is to snag this iPhone GUI Photoshop file that contains lots of UI elements in PSD layers, then change the tint of the large button in Photoshop and save it as a PNG.
One advantage of doing it this way is that you can also create versions for button selected and/or highlight state and assign the images to a standard UIButton.
You can create a separate section in your grouped table view, give that section only one row, and set that cell's background image to a red gradient image. You'll have to recreate that image on your own, though.
I would like to contribute a solution which does not use images but which gives the same look as the 'delete' button in Contacts.
In the below example I will use assume a UITableView with grouped, static cells, designed in storyboard. Make one of the sections have only a single cell. That cell will be the 'delete' button. Give the cell a red background color (f.e. Red 221, Green 0, Blue 2)
What we will do is add two GradientLayers to the cell. The first will cover the upper half of the cell. The second will cover the lower half.
Add QuartzCore to your implementation file:
#import <QuartzCore/QuartzCore.h>
Start with making an outlet to this cell:
#property (strong, nonatomic) IBOutlet UITableViewCell *cellDelete;
Create a method in which the cell will be formatted:
- (void)formatCellDelete
{
// Top Gradient //
CAGradientLayer *gradientTop = [CAGradientLayer layer];
// Make a frame for the layer based on the size of the cells contentView
// Make it half the height
// The width will be -20 so the gradient will not overflow
CGRect frame = CGRectMake(0, 0, _cellDelete.contentView.frame.size.width - 20, _cellDelete.contentView.frame.size.height / 2);
gradientTop.frame = frame;
gradientTop.cornerRadius = 8;
UIColor* topColor = [UIColor colorWithWhite:1.0f alpha:0.75f];
UIColor* bottomColor = [UIColor colorWithWhite:1.0f alpha:0.0f];
gradientTop.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
[_cellDelete.contentView.layer setMasksToBounds:YES];
[_cellDelete.contentView.layer insertSublayer:gradientTop atIndex:0];
// Bottom Gradient //
CAGradientLayer *gradientBottom = [CAGradientLayer layer];
// Make a frame for the layer based on the size of the cells contentView
// Make it half the height
// The width will be -20 so the gradient will not overflow
frame = CGRectMake(0, 0, _cellDelete.contentView.frame.size.width - 20, _cellDelete.contentView.frame.size.height / 2);
// And move to bottom
frame.origin.y = frame.size.height;
gradientBottom.frame = frame;
topColor = [UIColor colorWithWhite:0.0f alpha:0.05f]; //0.20
bottomColor = [UIColor colorWithWhite:0.0f alpha:0.0f];
gradientBottom.colors = [NSArray arrayWithObjects:(id)[topColor CGColor], (id)[bottomColor CGColor], nil];
[_cellDelete.contentView.layer setMasksToBounds:YES];
[_cellDelete.contentView.layer insertSublayer:gradientBottom atIndex:0];
// Define a selected-backgroundColor so the cell changes color when the user tabs it
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor colorWithRed:(float)(0.502) green:0.0 blue:0.0 alpha:1.000]];
bgColorView.layer.cornerRadius = 10;
[_cellDelete setSelectedBackgroundView:bgColorView];
}
The above will give your cell the glass-look like the 'delete' button in Contacts.
But we also want it to change color when the user taps it. This is what the last piece of code in the above method will do. It will set a different view with a darker color as the selectedBackgroundView.
After tapping the cell will stay selected and will keep its dark color. To automatically deselect the cell we do the following:
Start with a constant which defines the section nr of your delete cell:
static NSInteger const SECTION_DELETE = 1;
Now implement the (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method (defined in UITableViewDelegate):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == SECTION_DELETE){
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
// Navigation logic may go here. Create and push another view controller.
/*
 *detailViewController = [[ alloc] initWithNibName:#"" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}