I have created a sample picker-view which will display some details. Setting the Background color using:
_pickerView.backgroundColor = [UIColor redColor];
doesn't seems to be working . But I wish I could create a picker view like the below image
What I was trying to do is , the entire PickerView background color should not be white, it should fill with the UIColor which I am giving. Is that possible?? How it can be done ??
Addition to Nina's answer, below are some of the good custom Picker view controls which will be good to use in terms of performance and customizable.
http://www.cocoacontrols.com/platforms/ios/controls/cppickerview
http://www.cocoacontrols.com/platforms/ios/controls/afpickerview
http://www.cocoacontrols.com/platforms/ios/controls/v8horizontalpickerview (Horizontal PickerView)
I wrote a custom picker view that uses a UITableView as it's starting point. Feel free to customize it however you want.
https://github.com/derekju/DjuPickerView
You can change selected color by putting tableview cell in it.
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;
if( cell == nil ) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
[cell setBackgroundColor:[UIColor redColor]];
[cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
cell.textLabel.text = [self.userNameArray objectAtIndex:row];
cell.userInteractionEnabled = NO;
}
return cell;
}
It is hard to customize a UIPickerView.
You might opt to use it in its default appearance or trying to simulate a UIPickerView using another UIKit component such as UITableView. There are some libs that are using UITableView to simulate a more flexible UIPickerView.
UIPickerView has subviews. Assigning an image to a view and adding it to the subview at index 2, would change the background of pickerview.
I had had a same kinda problem :-) See my question here!! Customize UIPickerView's Skin with images
Or you can use AFPickerView
The API does not provide a way to change the style and color of selection dialer. You can change only inner view of the picker how u want to design.
As the UIPickerView don't have the UI_APPEARANCE_SELECTOR.
Check out below link will help you out.
Custom iOS UIDatepicker using UIAppearance
Related
In my TableView I use static cells. I take one section and in this section there are five cells(static cells). Now I want to make the whole section or these five cells transparent. That means I can see the Table View Controller background through the cells. How can I do that?
I have gone through many solutions. But not solved.
Edit:
May be many of you are not clear about my question. I want the cell like the below image. it is downloaded image and there will be section which is not available in this image. I am just using this image to clear the question. Look at the image the cell background is transparent.
To achieve this in the most simple way possible:
In storyboard select the static cells you want to appear transparent and select the background colour of 'Group table view background colour' or R235,G235,B241.
Then move the separator (the line in between) by moving it off screen by setting the below settings.
If you still want the line ignore the above or if you want to change its colour use
override func viewDidLoad() {
super.viewDidLoad()
tableView.separatorColor = //ADD COLOR HERE
}
Then you can add content to these cells as you would normally as I have added the label and UIImage View above.
!!However, the less simple but best way to achieve this is by creating custom cells and using dynamic prototypes. You can find many tutorials on how to do this online.
you have to clear color to contentview of cell,also to the tableview cell and now i think you will be able to see the background and check now
just add below code for tableview
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] init];
headerView.backgroundColor = [UIColor clearColor];
return headerView;
}
Override your TableViewCell's awakeFromNib to set it's own background color and it's contentView's background color to clearColor:
class MyTableViewCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = .clear
self.contentView.backgroundColor = .clear
}
}
You need to set tableView with clear background color. Also tableView cell and contentView backGround color as Clear color.
For this add this Line in viewDidLoad Methods.
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundColor:[UIColor clearColor]];
Add this line in cellForRowAtIndexPath Method.
cell.backgroundColor=[UIColor clearColor];
cell.contentView.backgroundColor=[UIColor clearColor];
1Hi in my project there is a requirement to show table row selection(blue color) only half of the row.Is there any way to do this.
The selected cell should be in blue color and remaining all are in white. Please help me to achive this also.I dont want to reload table because many images are there loading from server.If reload again again it will slow down performance.
[Please see the image after selection the cell should appear like this]
enter image description here
You can set selectedBackgroundView property of UITableViewCell with a view whose half is blue.
In didSelectRowAtIndexpath get cell ..
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *myCell = [tableView cellForRowAtIndexPath:indexPath];
// add view as per your according to need in cell
UIView * myCellAccessoryView = [UIview alloc] init];
// set frame of myCellAccessoryView as per your need ..
myCellAccessoryView.backGroundColor = // set color as you need .
// and add in cell
[cell.contentView addSubView :myCellAccessoryView];
}
hope it helps you ..
You can use this
UIView *backView = [[UIView alloc]init];
backView.frame = CGRectMake(0, 0, cell.frame.size.width/2, cell.frame.size.height);
backView.backgroundColor =[UIColor blueColor];
[cell addSubview:backView];
This will create a blue color background view.
I'm currently writing an App which uses different selection Colors in a grouped Table View. This works great as far as it comes to Borders.
I'm able to change Selection Color with the Code in this Post:
How to customize the background/border colors of a grouped table view cell?
BUT I'd like to additionally have the nice Border around the Cells that Apple uses. How can I use this Border?
http://www.mediafire.com/?x2gxbkjqu4d2zto
This is the Code to create the Background - mostly copied from above Post:
http://www.mediafire.com/?kltwlni1mf4t7ks
And this is how I use it:
NSIndexPath * indexPath = [[[NSIndexPath alloc] initWithIndex:0] indexPathByAddingIndex:1];
CGRect frame = [[self tableView] rectForRowAtIndexPath:indexPath];
TLCustomCellBackgroundView * selectedBackgroundView = [[TLCustomCellBackgroundView alloc] initWithFrame:frame andColor:[UIColor redColor]];
[selectedBackgroundView setPosition:CustomCellBackgroundViewPositionBottom];
[[[self tableView] cellForRowAtIndexPath:indexPath] setSelectedBackgroundView:selectedBackgroundView];
As you can see I nearly got it working correctly except of the gray Border around the second Cell.
in cellForRowAtIndexPath you can set the Borders (Separator) for each row/section.
if(indexPath.row == 1)
[tableView setSeparatorColor:[UIColor redColor]];
if(indexPath.section == 2)
[tableView setSeparatorColor:[UIColor greenColor]];
Just remember to set/reset this for each row/section you want it.
If you want to add the gray border around your red cell it would be
if(indexPath.section == 0 && indexPath.row == 1){
//assuming this is the correct index)
[tableView.separatorColor = [UIColor colorWithRed:0.67 green:0.67 blue:0.67 alpha:1];
}
else
{
[tableView.separatorColor = [UIColor yourChosenDefaultColor]];
}
You actually need to use this in your cellForRowAtIndexPath or you're going to set the border for your whole TableView (maybe thats what you want to do)
I found a Solution to this. it is not totally the same (as UITableviewCell in grouped Style got a even more sophisticated Design in iOS5 but this is sufficient to me. I borrowed this Approach from
http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html
and it works great!
The code I used is: http://www.mediafire.com/?mdxkpfe8g74ctk9
Hope this helps others
I have a UItableview in my IOS app with some information in it. I changed the Selected background color to clearcolor using the following code:
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];
[cell setSelectedBackgroundView:bgColorView];
[bgColorView release];
There is text and 2 images in the cell, I've build this using CGRectMake.
But When I select and hold a table cell the the images disappear behind what looks like a white background.
As you can see I'm holding the "Dacnusa sibrica" cell, how can I fix this?
If you want to disable the blue selection of cells you can also set the cell's selection mode instead of modifying the background:
cell.selectionStyle = UITableViewCellSelectionStyleNone
This simply disables the blue selection when tapping the cell but still allows the cell to be selected (and thus handled by code).
With Auto layout it is possible to define Selection=None (default is gray) in the Interface Builder.
have you used [cell.contentView addSubview:image]?
use [tableView deselectRowAtIndexPath:indexPath animated:YES];
for deselecting the cell
Why doesn't UITableViewCell background color work (set in interface builder)?
I note from some searching that the follow code set in your custom subclass of UITableViewController does work (see below):
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [UIColor redColor];
}
But I would still like to just understand why interface builder has a background color setting for the TableViewCell, which effectively doesn't seem to work?
It's a bit late, but I just ran into this issue... Setting the background color on the contentView works fine:
cell.contentView.backgroundColor = [UIColor redColor];
What worked for me is creating my own UIView object to be set as the background view of the UITableViewCell. In the cellForRowAtIndexPath:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView* bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
bgview.opaque = YES;
bgview.backgroundColor = [UIColor orangeColor];
[cell setBackgroundView:bgview];
Trying to set the background colour of the UITableViewCell itself is not how you should be doing it. A table cell has a collection of five useful views you can access, one of which is backgroundView.
Recommended reading:
http://cocoawithlove.com/2009/04/easy-custom-uitableview-drawing.html
and then:
http://cocoawithlove.com/2010/12/uitableview-construction-drawing-and.html
You should always provide changes for cells in tableView:willDisplayCell:forRowAtIndexPath: instead of the tableView:cellForRowAtIndexPath: method, as per Apple Documentation.
This Works !!
[cell.textLabel setBackgroundColor:[UIColor clearColor]];
[cell.contentView setBackgroundColor:[UIColor redColor]];
Make sure before defining a cell color your backgroundView is null
cell.backgroundView = nil;
cell.backgroundColor = [UIColor redColor];
This is simple and works: in interface builder, add a view object to the UITableViewCell of the same dimensions, and place your controls inside that view. Then you get whatever background color you want.
Edit: here's a reason to accept, it just struck me: take a careful look at what you get when you double click on a UITableViewCell in IB that you place into your nib, to start adding stuff to it: you don't get a plain rectangle looking view, you get an oval rectangle that says quite clearly: Content View. So all you can do is set the content view of the UITableViewCell in IB, and setting the background color property of the content view does not produce the color change in the table. As the docs say, UITableViewCells are complex beasts with many parts with a lot going on behind the scenes in terms of the UITableView adjusting it and fiddling with it.
It works perfectly, if you pick the tableview and set background to for example red the whole table will be red.
maybe you do not linked the table or something
hope it helped
(try this)
Apple's IB's design is generic to be applied to multiple kinds of components, and is also targeted at a highly technical audience (developers). Hence it has not been designed (as an application) to fully customise the interface properly for each component/situation type. So in this case it is the case the ability to seemingly set the background-color is somewhat misleading.
Y not, use setBackgroundColor
self.table.separatorColor=[UIColor whiteColor];
[table setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
You can partially do this in Interface Builder (probably added to Xcode recently):
Expand the cell in the Storyboard's Document Outline and select "Content View"
Set Content View background color in the Attributes Inspector
Select each content item in the cell and set their background colors
to ClearColor
Still don't see a way to set the accessory background though.
I did get this issue also.
In IB you get the ability to change the background color of the cell when choosing cell. This is NOT correct. You should go to the document outline on the left of the storyboard and choose the content view inside the cell. Change the background of the content view. You should now get correct cell color.
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
UIView *view = [[UIView alloc] init];
[view setBackgroundColor:[UIColor redColor]];
[cell setSelectedBackgroundView:view];}
We need to change the color in this method.
Try this it works for me:
- (void)tableView:(UITableView *)tableView1 willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setBackgroundColor:[UIColor clearColor]];
tableView1.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: #"Cream.jpg"]];
}