Reputation: 379
I am using UIDatePicker
and UIPickerView
in my app. But both the controls are showing weird behaviour. When I scroll the content in the picker, it flows outside the picker and does not scroll as it is supposed to. My implementation is as below.
sexArray = @[@"Male", @"Female"];
UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, 100, 150)];
[picker setDataSource: self];
[picker setDelegate: self];
picker.showsSelectionIndicator = YES;
genderTF.inputView = picker;
#pragma mark -- UIPICKERVIEW METHODS
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return sexArray.count;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel *retval = (UILabel*)view;
if (!retval) {
retval = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100, [pickerView rowSizeForComponent:component].height)];
}
retval.font = [UIFont fontWithName:@"SourceSansPro-Regular" size:15.0f];
retval.minimumScaleFactor = 0.6;
[retval setTextAlignment:NSTextAlignmentCenter];
retval.text = [sexArray objectAtIndex:row];
return retval;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
[genderTF setText:sexArray[component]];
}
When the same code is used in another app (for testing), it works as expected.
Upvotes: 0
Views: 118
Reputation: 1
I found this problem cause I used a third-party lib called "SLPagingView", I don't know whether you used it, too. If you did, this may help. I've just solved this problem by modifying the -(void)setFrame:(CGRect)frame and -(void)updateConstraints methods in the "UIScrollView+UpdateContentSize.m" file, and here is my code:
#pragma mark - OVERRIDE
-(void)setFrame:(CGRect)frame {
[super setFrame:frame];
// Scale the content size
Class UIPickerTableView = NSClassFromString(@"UIPickerTableView");
if ([self class] == UIPickerTableView) {
return;
}else {
[self updateContentSize];
}
}
-(void)updateConstraints {
[super updateConstraints];
// Scale the content size
Class UIPickerTableView = NSClassFromString(@"UIPickerTableView");
if ([self class] == UIPickerTableView) {
return;
}else {
[self updateContentSize];
}
}
This works and wish it could help you too.
Upvotes: 0
Reputation: 379
Turns out that the implementation was correct on my part. The problem was because of a third party library.
I was using a pod SLPagingView
in my project which made the `UIPickerView' act weird. I couldn't find a solution and ended up in replacing the pod with similar pod.
Details link
Upvotes: 2
Reputation: 27438
You need to implement titleForRow
instead of viewForRow
.
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
return [sexArray objectAtIndex:row];
}
Update :
If you are using picker view as input view
of textfield
then don't init it with frame.
Init picker view like,
UIPickerView *picker = [[UIPickerView alloc] init];
Hope this will help :)
Upvotes: 0