Note that there are some explanatory texts on larger screens.

plurals
  1. POFixed labels in the selection bar of a UIPickerView
    primarykey
    data
    text
    <p>In the clocks application, the timer screen shows a picker (probably a <code>UIPicker</code> in <code>UIDatePickerModeCountDownTimer</code> mode) with some text in the selection bar ("hours" and "mins" in this case).</p> <p>(edit) Note that these labels are <strong>fixed</strong>: They don't move when the picker wheel is rolling.</p> <p>Is there a way to show such fixed labels in the selection bar of a standard <code>UIPickerView</code> component?</p> <p>I did not find any API that would help with that. A suggestion was to add a <code>UILabel</code> as a subview of the picker, but that didn't work.</p> <hr> <p><strong>Answer</strong></p> <p>I followed Ed Marty's advice (answer below), and it works! Not perfect but it should fool people. For reference, here's my implementation, feel free to make it better...</p> <pre><code>- (void)viewDidLoad { // Add pickerView self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; [pickerView release]; CGSize pickerSize = [pickerView sizeThatFits:CGSizeZero]; CGRect screenRect = [[UIScreen mainScreen] applicationFrame]; #define toolbarHeight 40.0 CGFloat pickerTop = screenRect.size.height - toolbarHeight - pickerSize.height; CGRect pickerRect = CGRectMake(0.0, pickerTop, pickerSize.width, pickerSize.height); pickerView.frame = pickerRect; // Add label on top of pickerView CGFloat top = pickerTop + 2; CGFloat height = pickerSize.height - 2; [self addPickerLabel:@"x" rightX:123.0 top:top height:height]; [self addPickerLabel:@"y" rightX:183.0 top:top height:height]; //... } - (void)addPickerLabel:(NSString *)labelString rightX:(CGFloat)rightX top:(CGFloat)top height:(CGFloat)height { #define PICKER_LABEL_FONT_SIZE 18 #define PICKER_LABEL_ALPHA 0.7 UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE]; CGFloat x = rightX - [labelString sizeWithFont:font].width; // White label 1 pixel below, to simulate embossing. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, top + 1, rightX, height)]; label.text = labelString; label.font = font; label.textColor = [UIColor whiteColor]; label.backgroundColor = [UIColor clearColor]; label.opaque = NO; label.alpha = PICKER_LABEL_ALPHA; [self.view addSubview:label]; [label release]; // Actual label. label = [[UILabel alloc] initWithFrame:CGRectMake(x, top, rightX, height)]; label.text = labelString; label.font = font; label.backgroundColor = [UIColor clearColor]; label.opaque = NO; label.alpha = PICKER_LABEL_ALPHA; [self.view addSubview:label]; [label release]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload