简体   繁体   中英

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

I want to format the iOS 7 UIPickerView to display only three choices. Ie 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?

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM