UIPicker With View + View Tapped != Selected Row - iphone

Hey! I have a UIPicker, and I am using the function:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
But, when you tap one of the views returned by this function, the tapped view does not become highlighed, and selected if the touch is released. How can I fix that?

Make use of this property,
pickerView.showsSelectionIndicator = YES;
at initialization.

Related

Disable delegate methods depending on iOS version

I'm using some UIPickerView delegate methods only for the brand-new-Ive-modified iOS under NDA, e.g.
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
I don't want to implement them for iOS6, to preserve default iOS behavior, so a condition inside the method won't work. How to do it?
In your delegate, override respondsToSelector: and return NO in case of old OS:
- (BOOL)respondsToSelector:(SEL)selector
{
if (selector == #selector(pickerView:viewForRow:forComponent:reusingView:))
return MyFunctionThatChecksIosVersionIsMinimum7();
return [super respondsToSelector:selector];
}
So you're implementing the delegate method normally. When the picker view is asking your delegate if it understands the message you're simply lying to it.

Set font as per width

Hello friends I am displaying text in UIPickerview row. But when the label size is too big then Picker row displays ... at the end of the label.
I want to display whole title in row of Picker instead of this. Can i set the font size automatically as per the title width in pickerview.
You need to implement following method:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
}
// Fill the label text here
...
return tView;
}
Make your controller a UIPickerViewDelegate and UIPickerViewDataSource, then you can use this routine to make any custom change you want for your picker row:
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
//make your own view here, it can be a UILabel if you want, or other kind of view.
//You can give it any size font you need.
}

Custom UIPickerView

I want to use custom picker view that will add a check sign in front of selected row.
I have used apple sample code for custom UIPickerView on the UICatalog example. I was able to add the check sign for a given row when creating the picker. But I failed to add it when the user rotate the wheel to select new row and also to remove it from the previously added row. Any help would be appreciated.
Thanks,
1) Create subclass of UIView that will represent row in picker. Define property, for example, isChecked, which will show/hide checkmark on this view
2) In the – pickerView:didSelectRow:inComponent: call – viewForRow:forComponent: for previously selected row, set isChecked=NO
3) call – viewForRow:forComponent: for currently selected row and set isChecked=YES;
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
MyCustomRowView *prevRowView = [pickerView viewForRow:currentlySelectedRow forComponent:component];
prevRowView.isChecked = NO;
MyCustomRowView *currentRowView = [pickerView viewForRow:row forComponent:component];
currentRowView.isChecked = YES;
//then save currently selected row
currentlySelectedRow = row;
}
4) you also should check currently selected row when you is requested for it:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
....
//Create or reuse view
....
rowView.isChecked = (row == currentlySelectedRow);
}
As you are not providing any code, all we can give you is a general advise;
make sure the viewController showing your UIPickerView instance is inheritating the UIPickerViewDelegate-protocol
set the delegate of your UIPickerView towards this viewController; e.g. pickerView.delegate = self;
implement - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
within the above implementation, remove any checkmark previously added and add a new one to the row selected.

UIPickerView Font

Does anyone know if it is possible to change the font and/or color for individual UIPickerView Items?
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
// Setup label properties - frame, font, colors etc
...
}
// Fill the label text here
...
return tView;
}
Yes, it is possible. To do that you need to implement pickerView:viewForRow:forComponent:reusingView: method in picker's delegate - create UILabel instance there and setup it with whatever attributes you want.
For this, I would simply go with attributed strings.
Implement this method is your delegate:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView
attributedTitleForRow:(NSInteger)row
forComponent:(NSInteger)component

iphone uipickerview with images

it's possible add as item in the uipickerview some images/icons instead of text?
thanks
yes it is absolutely possible. u need to handle pickerView delegate method
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view
here you simply return custom view(could be anything UIImage UILabel)
and set userInteractionEnable property to no for customize view..