objective-c - UIPickerView viewForRow UILabel 忽略框架

标签 objective-c ios uipickerview

我正在使用 UILabel 作为我的 UIPickerView 的自定义 View ,并且我试图将标签从左侧填充 10px 左右。但是,无论我将 UILabel 设置为哪个框架,它都会被忽略。

我基本上是在尝试制作一个日期选择器,在年份组件中有一个“未知”选项。我是 iOS 开发的新手。子类化 UIDatePicker 并添加“未知”选项是否可能/会更优雅?

这是我的代码:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel* tView = (UILabel*)view;

    if (!tView)
    {
        tView = [[UILabel alloc] initWithFrame:** Any CGRect here **];

        tView.backgroundColor = [UIColor redColor];
        tView.font = [UIFont boldSystemFontOfSize:16.0];

        if (component == 0)
        {
            tView.textAlignment = NSTextAlignmentCenter;
        }
    }

    // Set the title
    NSString *rowTitle;

    if (component == 0)
    {
        rowTitle = [NSString stringWithFormat:@"%d", (row + 1)];
    }
    else if (component == 1)
    {
        NSArray *months = [[NSArray alloc] initWithObjects:@"January", @"February", @"March", @"April", @"May", @"June", @"July", @"August", @"September", @"October", @"November", @"December", nil];
        rowTitle = (NSString *) [months objectAtIndex:row];
    }
    else if (component == 2)
    {
        if (row == 0)
        {
            rowTitle = @"- Unknown -";
        }
        else
        {
            NSDateFormatter *currentYearFormat = [[NSDateFormatter alloc] init];
            currentYearFormat.dateFormat = @"YYYY";
            NSInteger currentYear = [[currentYearFormat stringFromDate:[NSDate date]] intValue];

            rowTitle = [NSString stringWithFormat:@"%d", (currentYear - row)];
        }
    }

    tView.text = rowTitle;

    return tView;
}

谢谢!

最佳答案

不要使用 UILabel直接地。对你来说最简单的方法是...

通过...定义宽度/高度

  • pickerView:widthForComponent:
  • pickerView:rowHeightForComponent:

  • ...而不是基于 UIView 创建自定义类并返回这个对象。在您的自定义 UIView , 添加 UILabel subview 和移动UILabellayoutSubviews你的类(class)。像这样的东西...
    // MyPickerView.h
    @interface MyPickerView : UIView
      @property (nonatomic,strong,readonly) UILabel *label;
    @end
    
    // MyPickerView.m
    @interface MyPickerView()
      @property (nonatomic,strong) UILabel *label;
    @end
    
    @implementation MyPickerView
      - (id)initWithFrame:(CGRect)frame {
        self = [super initWithFrame:frame];
        if ( self ) {
          _label = [[UILabel alloc] initWithFrame:CGRectZero];
        }
        return self;
      }
    
      - (void)layoutSubviews {
        CGRect frame = self.bounds;
        frame.origin.x += 10.0f;
        frame.size.width -= 20.0f;
        _label.frame = frame;
      }
    @end
    

    ...并返回您的MyPickerViewpickerView:viewForRow:forComponent:reusingView: .

    关于objective-c - UIPickerView viewForRow UILabel 忽略框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12847219/

    相关文章:

    objective-c - 在没有删除按钮的情况下重新排序 TableView 单元格并实现滑动以删除行

    iPhone编程-后台保存核心数据

    ios - 在自动布局中,无论方向如何,如何让 View 占据屏幕的一半?

    ios - Canvas SwiftUI 崩溃

    ios - 哪种格式最适合上传视频和音频

    ios - 将 UIDatePicker 用于公历以外的日历

    objective-c - Float 和 int 数据类型影响性能

    ios - Swift - AVAudioPlayer 不工作

    ios - 同一个 ViewController 上的 2 个 PickerViews - titleForRow 基金 "will not be executed"

    iphone - 如何在 UIActionSheet 中制作 UIPickerView