UiPickerView具有自定义固定标签和自动布局 [英] UiPickerView with custom fixed label and autolayout

查看:1534
本文介绍了UiPickerView具有自定义固定标签和自动布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现 UIPickerView 来选择小时,分钟和秒。我需要有一个标签,旁边的每个组件,当选择器旋转保持固定。例如,您可以查看Apple Clock应用程序的计时器部分。 I put a image for reference

I need to implement a UIPickerView to choose hours, minutes and seconds. I need to have a label, next to each component that stay fixed when the picker spin. For example you can look at the timer section of the Apple Clock app. I put an image for reference

当然我需要一个3个组件的选择器,但问题是一样的。我发现了很多解决方案,添加一个标签作为一个子视图,并使用一些框架和手动调整,但不能使它与AutoLayout的工作,我没有找到一个解决方案在网络上的位置。任何人都解决了这个问题?感谢

of course I need a picker with 3 components but the problem is the same. I found a lot of solution to add a label as a subview and position it using some frames and manual adjustment but a can' t make it work with AutoLayout and i don' t find a solution on the web. Anyone has solved this problem? thanks

推荐答案

我在一个应用程序中遇到过类似的问题,并决定如下:

I faced a similar problem in an App I worked on and decided on the following:

i。创建UIPicker

i. Create the UIPicker

ii。找到显示的选取器行的中点。

ii. Find the midpoint of the presented Picker rows.

iii。在屏幕上添加两个UILabels,并在 - (void)layoutSubview; 中确保标签总是位于中点的旁边。

iii. Add two UILabels to the screen and in - (void)layoutSubview; make sure the labels are always position next to the midpoints.

我只需要在我的项目中使用肖像模式支持,但应该适用于景观以及。只需选择足够宽,行宽,这样您就不会与选择器中的数据有任何重叠。这里是UIPicker的代码片段:

I only needed portrait mode support in my project, but should work fine for landscape as well. Just choose a wide enough, row width, such that you do not have any overlap with the data inside the picker. Here a snippet of code for the 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;
} 

这篇关于UiPickerView具有自定义固定标签和自动布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆