Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.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
Iphone 自定义UIPickerView(背景和间距)_Iphone_Sdk_Background_Uipickerview - Fatal编程技术网

Iphone 自定义UIPickerView(背景和间距)

Iphone 自定义UIPickerView(背景和间距),iphone,sdk,background,uipickerview,Iphone,Sdk,Background,Uipickerview,我想将UIPickerView中的白色背景更改为我自己的图像 这可能吗 此外,我还设法使UIPickerView水平滚动,而不是垂直滚动。现在,我想知道是否有任何方法可以调整选择器视图两行之间的间距 我附上了一张图片来说明我的意思 这是我的代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. arrayD

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

这可能吗

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

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

这是我的代码:

- (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:


在标签右侧添加一个空视图怎么样?对于后台,您可能应该使用默认后台视图作为第二个参数的
insertSubview:overview:
方法(如果您可以访问它)。

Hi 没有直接的方法可以改变背景。您可以做的是让您在viewForRow中返回的视图具有自己的背景(然后在需要时在每一侧添加阴影)。您也可以使用Tag:查找视图,但这绝不是一个好主意,因为这可能会在未来的iOS版本中发生变化

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

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

我通常从不自定义选取器,如果我需要一些不完全自定义的东西,我会将UITableView子类化,它会更灵活,并且可以实现选取器所做的+更多

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

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
返回一个大于视图的值


持有的;持有的;持有的;持有的

我刚刚知道如何在picker视图中应用背景色或图像。希望它能帮助别人

只需在文件中定义一个类别,您可以在其中查看选择器视图,如下所示:

@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

您可以进一步查看
子视图
,了解更多自定义信息。

看起来这是一个旧线程,但在iOS 7中(可能更早,我不确定),UIPickerViews具有背景色属性,因此设置背景色很简单:

[pickerView setBackgroundColor:[UIColor whiteColor]]

由于[UIColor WithPatternImage:]的存在,背景图像的设置也同样简单。(请注意,如果图像小于UIPickerView,则图案将平铺。)

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

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

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

非常感谢您提供的间距提示。太好了!我似乎不能像你提到的那样设置背景,因为背景视图之间会有差距。我相信这是由于间隔。此外,背景看起来很奇怪(它有两个“阴影”,应该只有一个在顶部)检查我的第一篇文章。如果你的背景图像不适合组件的大小,就会有一个间隙。您可以使背景与组件大小相同,也可以使用StretcableImage使UIImage适合组件。您是对的。我围绕标签、视图和图像的宽度播放,而不是视图的背景。不过,我在pickerview的两侧各有一个间隙。你知道有没有办法在那里设置背景?对于阴影效果,我可以在pickerview的顶部放置一个图像。那将是完美的,但我仍然需要背景本身。查看我的第一篇文章中的图片。这就是为什么我建议使用UITableView,pickerView是一个需要定制的噩梦。我想您必须在每一端用一个空组件填充数据,然后检查该组件是否被选中,以及当最后一个组件被选中时是否执行[pickerView selectRow:row-1 IncomComponent:0 animated:YES],并为第一个组件选择row:row+1。如果用户选择第一个或最后一个组件(填充),选择器将弹回到或弹回到有效组件。另一种挖出背景的方法是危险的,而且对经常发生的iOS变化敏感。这似乎有点冒险。随着视图堆叠,直到对象添加到UIPickerView,子视图的数量为零。如果你有更确切的想法,请告诉我。我似乎不能让它正常工作。
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;