I want to add a checkmark instead of the default SelectionIndicator for UIPickerView.
I am trying to add an image as subview
UIImageView * checkMark = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 102, 13, 13)] autorelease];
[checkMark setImage:[UIImage imageNamed:@"pickerChechmark.png"]];
checkMark.layer.borderColor = [[UIColor redColor] CGColor];
checkMark.layer.borderWidth = 2;
[picker addSubview:checkMark];
but its been added behind the picker see the following image.
image 1 for x =0, image 2 for x =5, image 3 for x = 10 (x position of checkmark) also see the red rectangle on top left, for clarification on my question
UIView * redblock = [[[UIView alloc] initWithFrame:CGRectMake(0, -20, 50, 40)] autorelease];
redblock.backgroundColor = [UIColor redColor];
[picker addSubview:redblock];
[picker bringSubviewToFront:redblock];
how fix this?
try this code,I have added a checkmark and label in my picker view
I also needed to customize the selection indicator because I populate the picker with custom views (they are similar to table view cells) that are higher than the standard indicator.
My initial attempt to specify a semi-transparent view to be superimposed to the UIPickerView upon creation led to the same result. While tracing I noticed that the picker view doesn't have subviews yet at the time of creation, which is probably the reason for the custom selection indicator be sent to the back of the visual hierarchy.
My solution consists of specifying the custom view for the selection indicator the first time the delegate method pickerView:viewForRow:forComponent:reusingView: is called. At that point in time the UIPickerView is ready with all its subviews and my new custom view is properly appended on top of the rest. If you don't need to use custom views for the contents, you could do the same using the other data source method pickerView:titleForRow:forComponent:.
The following snippet shows my solution.
Notes: if the delegate method serves more than one UIPickerView you need to handle the flag differently; SLPickerCell is my own cell class, just a UIView subclass.
Try adding the check mark to view itself like this:
[self.view addSubview:checkMark];
Your going to have to fiddle around with the coordinate but you should be able to see it on the picker view.