質問

I have this issue on iOS 7 with UIDatePicker. When I show a UIDatePicker in any view controller it gets a white background on the current date which will disappear if you roll the widget. I've tested with an empty new project with the same result (just new project, set the storyboard color to other than white color and drag any UIDatePicker to the scene and try like this). Does anybody know why is this behaviour? This control is supposed to be transparent and not have this white tag with it. enter image description here

役に立ちましたか?

解決

I had the same bug for the iPad version of the app. Somehow, the iPhone version was working fine. I found a workaround to remove the white background from the UIDatePicker.


Observation

  • The white background was showing for the first date and the date below it.
  • On changing the date (rolling), the white background would go away and would not come again.

Fix

To solve this problem, before showing the UIDatePicker in the view, I manually changed the date once and then re-set the date which is to be shown like this:

[datePicker setDate:[NSDate dateWithTimeIntervalSince1970:NSTimeIntervalSince1970]];
[datePicker setDate:[NSDate date]];
//Now add the datepicker to the view that you want to.

This seems to remove the annoying white background for me. Tell me if it works for you!

他のヒント

I had this problem myself. To fix it you need to take a UIView object and place it down where you want your UIDatePicker to be. Set the Background color for the UIView to White and make sure Opaque is Selected.

Next put your UIDatePicker inside of the UIView and connect any outlets or anything that needs to be connected with your UIDatePicker, from here run your Application and it should work.

Ok, I found a little tricky solution, but solution :) So it is the same problem as with UITableViewCell background color for iPad (iOS 7). The solution:

- (void)makeAllSubviewsBackroundClear:(UIView*)view
{
    for (UIView *subview in view.subviews)
    {
        if ([subview isKindOfClass:[UITableViewCell class]])
        {
            subview.backgroundColor = [UIColor clearColor];
        }

        [self makeAllSubviewsBackroundClear:subview];
    }
}

-(void)layoutSubviews
{
    [self makeAllSubviewsBackroundClear:self.datePicker];

    [super layoutSubviews];
}

The solution is to set "cell" background color to color you need, in our case - clear color, when the cell "appears". Maybe thats not an ideal solution, but I'll hope it will be helpful.

P.S.: you can refactor this code in someway to not call this method in every layout subviews but in special case.

I had similar problem with buggy background and solved it by resolving autolayout constraints. I assume UIDatePicker does not like it when it's constraints conflict with it's intrinsic content size. Try updating constraints in a way that your picker can resize itself to it's contents.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top