How do I format the iOS 7 UIPickerView to display only 3 choices? I.e. One above and one below the active choice?

StackOverflow https://stackoverflow.com/questions/22603290

  •  19-06-2023
  •  | 
  •  

Frage

I want to format the iOS 7 UIPickerView to display only three choices. I.e. one above and one below the selected option. I would also like to format the text to be smaller than the default. How do I achieve this?

War es hilfreich?

Lösung

Implementing these delegate methods will give you a picker view with 3 rows and let you customise the font, colour etc...

    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 1;
    }

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

        UILabel *pickerRowLabel = (UILabel *)view;

        if (pickerRowLabel == nil) {
            CGRect frame = //YOUR PICKERVIEW FRAME. HEIGHT IS 44 by default.
            pickerRowLabel = [[UILabel alloc] initWithFrame:frame];
           //FORMAT THE FONT FOR THE LABEL AS YOU LIKE
            pickerRowLabel.backgroundColor = [UIColor clearColor];
            pickerRowLabel.userInteractionEnabled = YES;
        }

        pickerRowLabel.text = //YOUR TEXT, MOST LIKELY FROM AN ARRAY ... ?

        return pickerRowLabel;
    }

    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

        return 3; //MOST LIKELY YOUR ARRAY OF OBJECTS COUNT
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top