UiPickerView具有自定义固定标签和自动布局

时间:2015-02-27 14:31:50

标签: cocoa uipickerview

我需要实现UIPickerView来选择小时,分钟和秒。我需要在拾取器旋转时保持固定的每个组件旁边都有一个标签。例如,您可以查看Apple Clock应用程序的计时器部分。我把图像作为参考

enter image description here

当然我需要一个带有3个组件的选择器,但问题是一样的。我找到了很多解决方案,可以将标签添加为子视图,并使用一些框架和手动调整来定位,但是可以使用使它适用于AutoLayout,我不会'在网上找到解决方案。有人解决了这个问题吗?感谢

3 个答案:

答案 0 :(得分:0)

我认为其他人可以从我的额外答案中受益。

对于i组件,以下内容应该在pickerView初始化方法(viewDidLoad)中,

    float fontSize = 20;
    float labelWidth = self.view.frame.size.width / [self.myPickerView numberOfComponents];
    float y = (self.myPickerView.frame.size.height / 2) - (fontSize / 2);
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelWidth* i, y, labelWidth, fontSize)];
    label.text = @"Label";
    label.font = [UIFont boldSystemFontOfSize:fontSize];
    label.backgroundColor = [UIColor clearColor];
    label.shadowColor = [UIColor whiteColor];
    label.shadowOffset = CGSizeMake (0,1);
    label.textAlignment = NSTextAlignmentRight;


    [self.myPickerView insertSubview:label aboveSubview:[self.myPickerView.subviews objectAtIndex:[self.myPickerView.subviews count] - 1]];

答案 1 :(得分:0)

Swift 4.2

enter image description here

在创建pickerView之后,添加下一个代码:

// Fixed labels
let font = UIFont.systemFont(ofSize: 20.0)
let fontSize: CGFloat = font.pointSize
let componentWidth: CGFloat = self.view.frame.width / CGFloat(agePickerView.numberOfComponents)
let y = (agePickerView.frame.size.height / 2) - (fontSize / 2)

let label1 = UILabel(frame: CGRect(x: componentWidth * 0.65, y: y, width: componentWidth * 0.4, height: fontSize))
label1.font = font
label1.textAlignment = .left
label1.text = "Years"
label1.textColor = UIColor.lightGray
agePickerView.addSubview(label1)

let label2 = UILabel(frame: CGRect(x: componentWidth * 1.65, y: y, width: componentWidth * 0.4, height: fontSize))
label2.font = font
label2.textAlignment = .left
label2.text = "Years"
label2.textColor = UIColor.lightGray
agePickerView.addSubview(label2)

let label3 = UILabel(frame: CGRect(x: componentWidth * 0.05, y: 0, width: componentWidth , height: fontSize))
label3.font = font
label3.textAlignment = .center
label3.text = "From"
label3.textColor = UIColor.lightGray
agePickerView.addSubview(label3)

let label4 = UILabel(frame: CGRect(x: componentWidth * 0.95, y: 0, width: componentWidth , height: fontSize))
label4.font = font
label4.textAlignment = .center
label4.text = "To"
label4.textColor = UIColor.lightGray
agePickerView.addSubview(label4)

答案 2 :(得分:-1)

我在我工作的应用程序中遇到了类似的问题并决定了以下内容:

我。创建UIPicker

II。找到所呈现的Picker行的中点。

III。将两个UILabel添加到屏幕上并在 - (void)layoutSubview;中确保标签始终位于中点旁边。

我在项目中只需要肖像模式支持,但也应该适用于横向。只需选择足够宽的行宽,这样就不会与拾取器内的数据重叠。这里是UIPicker的代码片段:

#pragma mark - Picker View

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0) {
        return self.hourPickerData.count;
    } else {
        return self.minutePickerData.count;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    return 40;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 30;
}

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

    if(component == 0) {
        text = self.hourPickerData[row];
    } else {
        text = self.minutePickerData[row];
    }

    UILabel *label = (UILabel*)view;
    if(view == nil) {
        label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
        label.backgroundColor = [UIColor clearColor];
        label.text = text;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = LYTiT_HEADER_COLOR;
        label.font = [UIFont fontWithName:LT_HELVETICA size:16];
    }

    return label;
} 
相关问题