Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UIPickerView视图用于行UILabel忽略帧_Objective C_Ios_Uipickerview - Fatal编程技术网

Objective c UIPickerView视图用于行UILabel忽略帧

Objective c UIPickerView视图用于行UILabel忽略帧,objective-c,ios,uipickerview,Objective C,Ios,Uipickerview,我正在使用UILabel作为我的UIPickerView的自定义视图,并且我正在尝试将标签从左侧填充10px左右。但是,无论我将UILabel设置为哪个帧,它都会被忽略 我基本上是想做一个日期选择器,在年份组件中有一个未知的选项。我是iOS开发人员的新手。是否可以/是否可以将UIDatePicker子类化并添加未知选项 这是我的密码: - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forCo

我正在使用UILabel作为我的UIPickerView的自定义视图,并且我正在尝试将标签从左侧填充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子视图并在类的LayoutSubView中移动UILabel。像这样的

// 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
。。。并在pickerView:viewForRow:forComponent:reusingView:中返回MyPickerView。

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

通过…定义宽度/高度

pickerView:widthForComponent: pickerView:rowHeightForComponent: 。。。然后基于UIView创建自定义类并返回此对象。在自定义UIView中,添加UILabel子视图并在类的LayoutSubView中移动UILabel。像这样的

// 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

。。。然后在pickerView:viewForRow:forComponent:reusingView:.中返回MyPickerView。

对于这样一个简单的情况,在标签文本的开头加上几个空格不是更容易吗。不需要自定义视图。@maddy WTF?哦,天哪,这种黑客。。。如果字体会改变怎么办?如果…怎么办。。。正确地做……OP没有说明他们为什么要缩进。我只是提供了一个替代方案。谁知道呢?也许缩进和字体大小一起调整是合适的。因此,使用空格并不一定是一种不恰当的方法。如果没有更多信息,两种方法都可以。对不起,按空格缩进是疯狂的。类似于单词中空格的缩进。。。在那里人们不知道标签。伟大的代码,它修复了我的问题,谢谢!不过有一条评论。。。initWithFrame中缺少对[self addSubview:_label]的调用:对于这样一个简单的情况,在标签文本的开头加上几个空格不是更容易吗。不需要自定义视图。@maddy WTF?哦,天哪,这种黑客。。。如果字体会改变怎么办?如果…怎么办。。。正确地做……OP没有说明他们为什么要缩进。我只是提供了一个替代方案。谁知道呢?也许缩进和字体大小一起调整是合适的。因此,使用空格并不一定是一种不恰当的方法。如果没有更多信息,两种方法都可以。对不起,按空格缩进是疯狂的。类似于单词中空格的缩进。。。在那里人们不知道标签。伟大的代码,它修复了我的问题,谢谢!不过有一条评论。。。initWithFrame中缺少对[self addSubview:\u label]的调用: