I have observed that UIPicker always remains in black color,
Is there any way to change the color of UIPicker & it's Selection Indicator?
Thanks for helping me.
I assume all you want to change is the color of the border of the picker, not of the region in the center with which the user interacts. In this case, do the following:
Create 4 "cover" UIViews and add them directly to the UIPicker, as in:
[picker addSubview: coverView];
Position these views over the top, bottom, left and right sides of the picker border. (You will need to experiment with sizes.) Set the backgroundColor of the coverViews to the color you want, and adjust the alpha to get the gradient shading from the picker. Again, this may take a bit of experimentation.
The alternative would be to create one big coverView that covers the entire picker, and override the - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event method so that your cover view did not intercept the touches meant for the picker.
You can create your own custom pickers, check the UICatalog sample project at apples site, they show how to make a custom picker, it might help you https://developer.apple.com/iphone/library/samplecode/UICatalog/
Create a single overlay image as Amagrammer suggested but instead of adding it to your view as an image, add it as a button. Then disable the interaction on the button and the picker will receive the touch events without the need to intercept or override anything.
You can do this by adding 5 ImageView also. First one is of size "Selection Indicator" and put it exactly over "Selection Indicator". Now change that imageView's alpha to 0.2 or whatever you want and also add image/color as per your choice. Now, you will see your custom "Selection Indicator". Also, same thing you can do for borders. Add each imageView to four sides of pickerview and make it's size equal to border size. Now fill the images/color you want in that ImageView.
This is the best solution I have found
http://www.inexika.com/blog/Customizing-UIPickerView-UIDatePicker
You'll need to create a new UIView with a UIImageView inside it and then set that as the Accessory for the cell. So you'll need to create a image just like the default accessory but in the color you want.
UIView* accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 24, 50)];
UIImageView* accessoryViewImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"NEWIMAGE.png"]];
accessoryViewImage.center = CGPointMake(12, 25);
[accessoryView addSubview:accessoryViewImage];
[cell setAccessoryView:accessoryView];
[accessoryViewImage release];
[accessoryView release];
for changing text color here's the solution
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
CGRect imageFrame = CGRectMake(0.0, 0.0, 15, 15);
UIImageView *label = [[[UIImageView alloc] initWithFrame:imageFrame] **autorelease**];
if (row == 0)
{
label.backgroundColor = [UIColor redColor];
}
if (row == 1)
{
label.backgroundColor = [UIColor blueColor];
}
if (row == 2)
{
label.backgroundColor = [UIColor blackColor];
}
return label;
}
Related
I am having trouble customizing the look and behavior of a subclassed tableview cell when it enters selected state.
My cell has three labels I added to its content view in the initWithStyle: method as such:
cell1Label = [[UILabel alloc] initWithFrame:
CGRectMake(75.0f, 12.0f, 67.0f, 12.0f)];
cell1Label.backgroundColor = [UIColor clearColor];
cell1Label.textColor = [UIColor blackColor];
cell1Label.shadowColor = [UIColor whiteColor];
blah, blah, blah...
[self.contentView addSubview:cell1Label];
Then, I put a black overlay on top of the background in the setSelected:(BOOL)selected animated:(BOOL)animated method within the subclass:
UIView *backgroundView = [[UIView alloc] initWithFrame:
CGRectMake(0.0f, 0.0f, 150.0f, 70.0f)];
backgroundView.backgroundColor = [UIColor colorWithRed:
0.0 green:0.0 blue:0.0 alpha:0.4];
self.selectedBackgroundView = backgroundView;
The problem start here. Because I want to keep my UILabel readable when the cell is selected, I need to change their textColor and shadowColor. However, I cannot seem to find a good place to do this.
If I put the code in the setSelected:(BOOL)selected animated:(BOOL)animated nothing happens; I can only seem to add changes to the selectedBackgroundView.
I also tried using the didSelectRowAtIndexPath: and didDeselectRowAtIndexPath: TableView delegate methods as such:
CustomDataCell* selectedCell = (CustomDataCell*)[tableView
cellForRowAtIndexPath:indexPath];
selectedCell.cell1Label.shadowColor = [UIColor lightGrayColor];
selectedCell.cell1Label.textColor = [UIColor blackColor];
This method, however, has some issues when cells leave the visible area. Namely, if I select a cell then it leaves the visible area, its text properties do not change back to their normal state when I select another cell. The black background disappears as it should, but the new textColor and shadowColor I assigned to the selected state persists.
What is the best, most reliable way to handle selected (and possibly other) states of subclassed UITableViewCells?
I am using ARC; never use IB; on Xcode 4.6 and iOS 6.1 SDK.
Use the setHighlighted:animated method of UITableViewCell to change your label color.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Change you label text color here
//
// Edit Here
if (selected) {
// New Colors Here
}
else {
// Old Colors Here
}
}
When a cell is selected, it will set all of its labels (including ones you've added yourself) to their highlighted state. This means you can define the highlighted text color at initialisation and let the cell take care of it.
Probably at the moment the labels are being set to highlighted which is undoing any colour changes you are making yourself.
Also, a cell has a selectedBackgroundView property which you should be using instead of adding a new subview.
I would like to know the best way/correct way to achieve from following layout? I want to place an UIImageView outside of UITableViewCell in a UITableView with static cells.
I have done the following by subclassing UITableViewCell for the cells in section 1 using the following code
- (void)setFrame:(CGRect)frame {
frame.size.width -= 125.0;
[super setFrame:frame];
}
In the UITableViewController's viewDidLoad I add the UIImageView and position it according to the width of the custom UITableViewCell.
This sort of works, but i'm not sure how to deal with rotation and also if what i've done so far would be the correct way?
there are differnt ways to do it. one is to set the width of table view less as you showd in pic 2nd is to use custom table view cell and on required cell add image so that your cell data as well as image will be shown. i think custom cell would be the better solution. tell me if you are asking the same thing what i answered, if no, then i review my answer thank.
I managed to produce what I wanted using the follow, this is proberly not the best way or cleanest way but as no one from StackOverFlow gave any better suggestions I thought I better answer this.
I subclassed the first 3 UITableViewCells and set a frame size to take into account the size of my image.
- (void)setFrame:(CGRect)frame {
float cellWidth = (frame.size.width - 345);
frame.size.width = cellWidth;
[super setFrame:frame];
}
Then in my UITableViewController's viewDidLoad I create and position the UIImageView using the first static cell in the tableview as an IBOutlet ("firstCell"). I then set the autoResizingMask which sorts out rotation and finally add the UIImageView to the view.
//Create and position the image view
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((firstCell.frame.size.width), (firstCell.frame.origin.y + 70), 300, 137)];
// Add border and round corners of image view to make style look a little like tableviewcells
[imageView.layer setBorderColor:[UIColor colorWithWhite:0 alpha:0.5].CGColor];
[imageView.layer setBorderWidth:2.0];
[imageView.layer setCornerRadius:5.0];
[imageView setClipsToBounds:YES];
//Set the image in the image view
UIImage *image = [UIImage imageNamed:#"defaultPicture.png"];
imageView.image = image;
//Set resizing of image view for when view is rotated
imageView.contentMode = UIViewContentModeRedraw;
imageView.autoresizingMask = UIViewContentModeScaleAspectFit;
//Add tap gesture for imageview to initiate taking picture.
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *imageViewTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(takePicture:)];
[imageView addGestureRecognizer:imageViewTap];
//Add image view to view
[self.view addSubview:imageView];
This has not been fully tested and i'm sure isn't a great implementation, but its a starting point for the effect i'm after. Please reply if you know of a better way.
Note: The above code is written for my app on the iPad but screenshots are from testing I did on iPhone
I have a table cell being displayed that shows a users image, name and some text. The user's image is 50x50, but I want a border around it so I set the view to center the image and set the frame to 52x52 and then set the background color of that view to my border color. That shows a 1 pixel border around the image.
I also want to show a 30 pixel wide border on the right of the cell when the cell is selected. I've tried to do that by creating a UIView the size of the cell's frame, then adding a subview to that view with a UIView the width and background color I would like. I then set that view to the selectedBackgroundView of the cell.
The problem here is that the cell's selectedBackgroundView gets applied to the background of all views inside the cell. So when I select a cell, the images "border" gets set to the cell's selected background color, the other 30px "border" I'm adding gets changed to that background color also.
Code inside my cellForRowAtIndexPath:
cell = (UserCellView *) currentObject;
UIView *c = [[UIView alloc ] initWithFrame:CGRectMake(0, 0, 30, cell.frame.size.height)];
c.backgroundColor = [[UIColor alloc] initWithRed:64/255.0 green:64/255.0 blue:64/255.0 alpha:1.0];
UIView *v = [[UIView alloc ] initWithFrame:cell.frame];
v.backgroundColor = [[UIColor alloc] initWithRed:35/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
[v addSubview:c];
cell.selectedBackgroundView = v;
[c release];
[v release];
I'll assume that you haven't actually tested what's going on to form your analysis that it "gets applied to the background of all views inside the cell".
I did something like this:
#interface TestView : UIView {
}
#end
#implementation TestView
-(void)setBackgroundColor:(UIColor*)c {
// Breakpoint here.
NSLog("setBackgroundColor: %#",c);
[super setBackgroundColor:c];
}
#end
...
UIView * v = [[[UIView alloc] initWithFrame:(CGRect){{0,0},{20,20}}] autorelease];
v.backgroundColor = [UIColor magentaColor];
UIView * v2 = [[[TestView alloc] initWithFrame:(CGRect){{5,5},{10,10}}] autorelease];
v2.backgroundColor = [UIColor yellowColor];
[v addSubview:v2];
cell.selectedBackgroundView = v;
The end result is that -setBackgroundColor: is called from -[UITableViewCell _setOpaque:forSubview:] when the view is selected, with something like UIDeviceWhiteColorSpace 0 0 (i.e. [UIColor clearColor]).
Or, in other words, the background colour of some of the subviews are set to [UIColor clearColor] while the cell is selected, allowing selectedBackgroundView to show through. I think this happens because a common optimization is to give textLabel/detailTextLabel the table's background colour (e.g. white) so it draws faster, but this means the background colour has to be reset when the cell is selected.
The easiest fix is to use an image instead: a 1-by-1-pixel image of the correct colour in a UIImageView will work, if a bit messy. (I had this problem when drawing custom separator lines with 1-pixel-high UIViews, so I just included the separator into the background image.)
An alternative fix is to use a CALayer instead: Add a 52x52 sublayer to the UIImageView's layer, and set the sublayer's background colour. I'm pretty sure UITableViewCell simply walks the view hierarchy, so it should ignore custom layers. (The big disadvantage with layers is that they don't auto-size, which made them unsuitable for my purposes, and means the 30px right border won't auto-size.)
A workaround is to subclass the relevant views and ignore -setBackgroundColor: if it's equal to [UIColor clearColor].
A simple but obnoxious-to-maintain solution is to override setSelected:animated: and setHighlighted:animated: with implementations re-setting the various backgrounds you want. Something along the lines of:
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
self.childView.backgroundColor = [UIColor blueColor]; // whichever you want
}
First add this to your file
#import <QuartzCore/QuartzCore.h>
Then turn your view into an image with...
UIView *rowView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 1.0, 60.0)];
rowView.backgroundColor = [UIColor colorWithRed:35/255.0 green:35/255.0 blue:35/255.0 alpha:1.0];
UIGraphicsBeginImageContext(rowView.bounds.size);
[rowView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *yourImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then instead of adding a UIView to your cell, just add a UIImageView with "yourImage".
A simple solution if the affected view can be a custom subclass is to override -setBackgroundColor:
- (void)setBackgroundColor:(UIColor *)color
{
// Ignore requests and do nothing
}
Thus UITableViewCell's attempt to set the colour will go ignored. Code in the custom view which really does want to set the background colour needs to call super:
- (void)setColor:(UIColor *)color
{
[super setBackgroundColor:color];
}
(or could probably message the underlying CALayer directly)
you will need to customize the contentView of the cells and handle the delegate tableView:cellForRowAtIndexPath
See Posting Here
How can I change the style of a UIPickerview. I want to change its white background to green color and its black frame to red color.
Anyone please help. I am a beginner.
As far as I know you can't actually change the frame's color, but you can create an image with a transparent area in the middle and add that on top of the frame like so:
[picker addSubview:image];
As for the background color, you can create your own custom views for each row by implementing the UIPickerViewDelegate method:
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component
reusingView:(UIView *)aView;
You could then set the background color of the view you return via
[myView setBackgroundColor:[UIColor yellowColor]];
However this only affects the rows with data. I don't think you can currently set the background color of the component itself which is a shame.
I personally prefer this:
[self.picker setBackgroundColor:[UIColor greenColor]];
self.picker.layer.borderColor = [UIColor whiteColor].CGColor;
self.picker.layer.borderWidth = 4;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
in this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
but I can only see it when I select that cell otherwise it's not visible.and it work perfectly when background is white.
I am sure that I need to set a property, but I don't know which property I need to change to make this thing work.
thanks in advance.
cheers.
I ran into this same issue, and just create a UIImageView out of a UIView’s imageWithName #"AccDisclosure.png" using the following hastily mocked-up graphic which you're free to copy: http://thinkingman.com/db/downloads/AccDisclosure.png (if you just click that link, you'll probably see nothing, as it's a white image with a transparent background, but if you save it and view against a dark background, you'll see the alpha).
It would appear that the disclosure indicator is a gray, high-alpha image, so overlaying that over a black background makes it invisible. If you want to do this, you'll need to add your own UIImageView to the cell's contentView.
The following code allows me to set the background color of the arrow tip in a table row:
#property (nonatomic,retain) UILabel *backgroundLabel;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = [UIColor orangeColor];
self.backgroundLabel = label;
[self.contentView addSubview:label];
[label release];
CGRect labelRect = CGRectOffset(contentRect,0, 0);
labelRect.size.height = contentRect.size.height - 1; // show white line
labelRect.size.width = contentRect.size.width + 50; // cover arrow tip background
backgroundLabel.frame = labelRect;
backgroundLabel.highlightedTextColor = [UIColor whiteColor];
I made a solution where I added an ImageView with addSubView in the normal fashion, with an image that was not black (in my case, a grey filled circle) at the position where the accessory appears.
That allows the arrow to be seen and still have a dark/black table cell background color.
Maybe not the most kosher solution, but it makes the arrow visible, and I get notified of accessory clicks without subclassing or writing lots of code.