gpt4 book ai didi

iphone - 修复了 UIPickerView 选择栏中的标签

转载 作者:行者123 更新时间:2023-12-03 18:14:47 25 4
gpt4 key购买 nike

在时钟应用程序中,计时器屏幕显示一个选择器(可能是 UIPickerModeCountDownTimer 模式下的 UIPicker),选择栏中包含一些文本(“小时”和“分钟” “在这种情况下)。

(编辑)请注意,这些标签是固定的:当拾取器轮滚动时它们不会移动。

有没有办法在标准 UIPickerView 组件的选择栏中显示此类固定标签?

我没有找到任何可以帮助解决这个问题的 API。有人建议添加 UILabel 作为选择器的 subview ,但这不起作用。

<小时/>

回答

我遵循了埃德·马蒂的建议(答案如下),它有效!并不完美,但它应该能骗过人们。作为引用,这是我的实现,请随意改进......

- (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];
}

最佳答案

创建您的选择器,创建一个带有阴影的标签,并将其推送到选择指示器 View 下方的选择器 subview 。

它看起来像这样


UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(135, 93, 80, 30)] autorelease];
label.text = @"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[picker insertSubview:label aboveSubview:[picker.subviews objectAtIndex:5]];
//When you have multiple components (sections)...
//you will need to find which subview you need to actually get under
//so experiment with that 'objectAtIndex:5'
//
//you can do something like the following to find the view to get on top of
// define @class UIPickerTable;
// NSMutableArray *tables = [[NSMutableArray alloc] init];
// for (id i in picker.subviews) if([i isKindOfClass:[UIPickerTable class]]) [tables addObject:i];
// etc...

-- 转发付款

关于iphone - 修复了 UIPickerView 选择栏中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/367471/

25 4 0