开发者

Is it possible to provide a UIView to a UIPickerView that 'fills' the row?

I am trying to provide custom views to a UIPickerView so that the entire background of each row is one of two colours (none of which are white).

I have been able to provide these custom views ok, with the colours I want, but I can't make the view fill the entire 'space' for the row.

The test code below is trying to achieve the effect of changing the background colour of the UIPicker to green - but the view I provide does not get aligned correctly. It is always a few pixels to the left and sometimes actually covers the left edge of the component depending on its width.

The effect of this is that I always see a white edge on the right hand side.

Changing the origin of the CGRect does not change its location in the row.

Changing the width of the CGRect changes the width ok but it is still always aligned a few pixels towards the left. i.e. not centered.

Can anyone please tell me wha开发者_运维百科t am I doing wrong here?

I just have a standard UIPicker dropped on to a view.

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
   return 44;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
   return 200;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
   UIView *rowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)] autorelease];
   rowView.backgroundColor = [UIColor greenColor];
   return rowView;  
}


Ok, I have managed to at least 'center' the view.

I had to add a subview to the view that is returned from viewForRow and try a few values for the origin.x value like so:

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

    UIView *rowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)] autorelease];

    UIView *subView = [[[UIView alloc] initWithFrame:CGRectMake(6, 0, 194, 44)] autorelease];
    subView.backgroundColor = [UIColor greenColor];
    [rowView addSubview:subView];

    return rowView;
}

For some reason though I have not been able to get the view to extend to the very edges of the row so the white edges that appear either side of the view remain. I can live with this.

Hope this helps someone else.


- (UIView *)pickerView:(UIPickerView *)apickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

    UIView *rowView = nil;

    if(view==nil) {
        rowView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
        rowView.backgroundColor = [UIColor greenColor];
        return rowView;
    }


    return view;  
}

the "trick" is in returning the reusingView if it is available. After this it's up to you to configure the view as you wish.

The "320" value is just here to test you should instead put some computed value such as the pickerView.frame.size.width so the code would be cleaner.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜