How to Make an Uipickerview Component Wrap Around

How do you make an UIPickerView component wrap around?

It's just as easy to set the number of rows to a large number, and make it start at a high value, there's little chance that the user will ever scroll the wheel for a very long time -- And even then, the worse that will happen is that they'll hit the bottom.

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
// Near-infinite number of rows.
return NSIntegerMax;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
// Row n is same as row (n modulo numberItems).
return [NSString stringWithFormat:@"%d", row % numberItems];
}

- (void)viewDidLoad {
[super viewDidLoad];

self.pickerView = [[[UIPickerView alloc] initWithFrame:CGRectZero] autorelease];
// ...set pickerView properties... Look at Apple's UICatalog sample code for a good example.
// Set current row to a large value (adjusted to current value if needed).
[pickerView selectRow:currentValue+100000 inComponent:0 animated:NO];
[self.view addSubview:pickerView];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSInteger actualRow = row % numberItems;
// ...
}

UIPickerView Wrap Around Swift 2

You can return a very large number in

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

and select the middle row while initialzing with

selectRow(COUNT/2, inComponent: 0, animated: true)

See this code sample

Wrapping of text in UIPickerView

When I set the custom font to the app by creating category class with UIFont, the text in the UIPickerView was showing in two lines.

@implementation UIFont (CustomFont)

+(UIFont *)regularFontWithSize:(CGFloat)size
{
return [UIFont fontWithName:GOATHAM_FONT size:size];
}

+(UIFont *)boldFontWithSize:(CGFloat)size
{
return [UIFont fontWithName:GOATHAM_FONT size:size];
}

+(void)load
{
SEL original = @selector(systemFontOfSize:);
SEL modified = @selector(regularFontWithSize:);
SEL originalBold = @selector(boldSystemFontOfSize:);
SEL modifiedBold = @selector(boldFontWithSize:);

Method originalMethod = class_getClassMethod(self, original);
Method modifiedMethod = class_getClassMethod(self, modified);
method_exchangeImplementations(originalMethod, modifiedMethod);

Method originalBoldMethod = class_getClassMethod(self, originalBold);
Method modifiedBoldMethod = class_getClassMethod(self, modifiedBold);
method_exchangeImplementations(originalBoldMethod, modifiedBoldMethod);
}

@end

Also, I set font in the UIPickerView delegate method.

 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

NSString *title = @"";

UILabel* tView = (UILabel*)view;
if (!tView){
CGRect frame = CGRectZero;
tView = [[UILabel alloc] initWithFrame:frame];
[tView setFont:[UIFont fontWithName:GOATHAM_FONT size:14]];
tView.minimumScaleFactor = 9.0f;
tView.adjustsFontSizeToFitWidth = YES;
tView.numberOfLines = 2;

[tView setText:title];
[tView setTextAlignment:NSTextAlignmentLeft];
}

return tView;
}

After doing this, text was getting shown in two lines in UIPickerView.

Can we enable looping of rows in a UIPickerView as we do it in UIDatePicker?

It is possible. For anyone of you who needs it,
try this.

Implement UIPickerView horizontally?

you can use CPPickerView .. A custom, configurable, horizontal version of UIPickerView (based on the spinning-wheel or slot-machine metaphor), with an included table cell implementation. Originally intended for condensing the space/rows needed for a multi-option setting.

Decrease the margin of content of UIPickerView ios

That is not possible. If you need differently styled "UIPickerView" you will to roll your own.



Related Topics



Leave a reply



Submit