首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >定制UIPickerView (背景和间距)

定制UIPickerView (背景和间距)
EN

Stack Overflow用户
提问于 2011-04-21 13:49:19
回答 4查看 13.5K关注 0票数 7

我想将UIPickerView中的白色背景更改为我自己的图像。

这个是可能的吗?

此外,我还设法使我的UIPickerView水平滚动而不是垂直滚动。现在,我想知道是否有任何方法来调整选择器视图的两行之间的间距?

我附上了一张图片来说明我的意思。

这是我的密码:

代码语言:javascript
运行
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    arrayDays = [[NSMutableArray alloc] init];
    [arrayDays addObject:@"ONSDAG"];
    [arrayDays addObject:@"TORSDAG"];
    [arrayDays addObject:@"FREDAG"];
    [arrayDays addObject:@"LØRDAG"];

    arrayDates = [[NSMutableArray alloc] init];
    [arrayDates addObject:@"29. JUNI"];
    [arrayDates addObject:@"30. JUNI"];
    [arrayDates addObject:@"1. JULI"];
    [arrayDates addObject:@"2. JULI"];

    pickerViewDay = [[UIPickerView alloc] initWithFrame:CGRectZero];
    [pickerViewDay setDelegate:self];
    [pickerViewDay setShowsSelectionIndicator:NO];
    CGAffineTransform rotate = CGAffineTransformMakeRotation(-M_PI/2);
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0);
    [pickerViewDay setTransform:rotate];
    [pickerViewDay setCenter:CGPointMake(self.view.frame.size.width/2, (pickerViewDay.frame.size.height/2)-3)];
    [self.view addSubview:pickerViewDay];

    // Adding selection indicator to pickerview
    UIImage *selectorImage = [UIImage imageNamed:@"DayPickerView_SelectionIndicator.png"];
    UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
    [customSelector setFrame:CGRectMake(0, 0, 120, 74)];
    [customSelector setCenter:CGPointMake(self.view.frame.size.width/2, customSelector.frame.size.height/2)];
    [self.view addSubview:customSelector];
    [customSelector release];

    // Adding background to pickerview
    UIImage *backgroundImage = [UIImage imageNamed:@"DayPickerView_Background.png"];
    UIView *custombackground = [[UIImageView alloc] initWithImage:backgroundImage];
    [custombackground setFrame:CGRectMake(0, 0, 320, 74)];
    // [self.view addSubview:custombackground];
    [custombackground release];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIView *viewRow = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 80)];

    CGAffineTransform rotate = CGAffineTransformMakeRotation(3.14/2);
    rotate = CGAffineTransformScale(rotate, 0.25, 2.0);

    // Date
    CGRect rectDate = CGRectMake(30, 0, 150, 80);
    UILabel *date = [[UILabel alloc]initWithFrame:rectDate];
    [date setTransform:rotate];
    [date setText:[arrayDates objectAtIndex:row]];
    [date setFont:[UIFont fontWithName:@"Arial-BoldMT" size:37.0]];
    [date setShadowColor:[UIColor whiteColor]];
    [date setShadowOffset:CGSizeMake(0, -1)];
    [date setTextAlignment:UITextAlignmentCenter];
    [date setBackgroundColor:[UIColor clearColor]];
    [date setClipsToBounds:YES];
    [viewRow addSubview:date];

    // Day
    CGRect rectDay = CGRectMake(-30, 0, 150, 80);
    UILabel *day = [[UILabel alloc]initWithFrame:rectDay];
    [day setTransform:rotate];
    [day setText:[arrayDays objectAtIndex:row]];
    [day setFont:[UIFont fontWithName:@"Arial-BoldMT" size:21.0]];
    [day setTextColor:[UIColor colorWithRed:0.35 green:0.35 blue:0.35 alpha:1]];
    [day setTextAlignment:UITextAlignmentCenter];
    [day setBackgroundColor:[UIColor clearColor]];
    [day setClipsToBounds:YES];
    [viewRow addSubview:day];

    return viewRow;
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [arrayDays objectAtIndex:row];
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayDays count];
}

编辑1

对于RickiG (背景):

编辑2

对于RickiG:

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-04-21 14:59:38

嗨,没有直接的方式来改变背景。您可以做的是让您在viewForRow特性中返回的视图具有它自己的背景(如果需要的话,随后在每一侧添加阴影)。您也可以去寻找viewWithTag:但这绝不是一个好主意,因为这可能会在未来的iOS版本中发生变化。

您实现viewForRow和TitleForRow有什么特殊的原因吗?我通常只是在这个委托方法中填充viewForRow的标签。

viewForRow能够重用选择器中的视图,就像UITableView一样,您应该测试"reusingView:(UIView *)视图“是否为零,如果没有,则无需再次绘制所有内容。只是填充标签。

我通常从不自定义选择器,如果我需要一些不是完全自定义的东西--我对UITableView进行了子类化,那么它就会更加灵活,并且可以做更多的选择器。

对于可以使用行的“高度”的间距,选择器将在viewForRow中对返回的视图进行居中,然后确保:

代码语言:javascript
运行
复制
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

返回大于视图的值。

(持有og lykke;)

票数 6
EN

Stack Overflow用户

发布于 2012-03-15 16:48:26

我刚刚想出了如何在选择器视图中应用背景颜色或图像。希望能帮上忙。

只需在要选择器视图的文件中定义一个类别,如下所示:

代码语言:javascript
运行
复制
@implementation UIPickerView(Extension)

-(void)drawRect:(CGRect)rect
{

    NSArray* subviews = [self subviews];
    for(UIView* view in subviews)
    {
        if([view isKindOfClass:[UITableView class]])
        {
            view.backgroundColor = appColor;
        }
    }
    [super drawRect:rect];
}

@end

您可以进一步查看subviews以获得更多的自定义。

票数 4
EN

Stack Overflow用户

发布于 2014-02-01 05:01:12

看起来这是一个旧线程,但是在iOS 7(可能更早的时候,我不确定),UIPickerViews有一个背景颜色属性,所以设置背景色很简单:

[pickerView setBackgroundColor:[UIColor whiteColor]];

由于UIColor colorWithPatternImage:,设置背景图像也同样简单。(请注意,如果图像小于UIPickerView,则模式将平铺。)

代码语言:javascript
运行
复制
[pickerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"someBackgroundImage.png"]]];

要修改组件宽度,可以实现委托方法widthForComponent,以便为每个组件设置不同的宽度。理论上,您应该能够添加一个空组件,并使用它的空白宽度来创建间隔,但我还没有尝试过这一点。

代码语言:javascript
运行
复制
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

我还没有找到一种方法来改变组件之间的填充,这似乎是修改间隔的一个更好的方法,并且还允许您删除组件之间的~5 px间距,但是如果我能够找到一个,我会更新我的答案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5744996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档