iOS 5.1 : -[UIColor colorWithPatternImage:] background color draws solid black

后端 未结 2 1391
既然无缘
既然无缘 2020-12-29 15:00

While testing my application in today\'s release of iOS 5.1 GM, I noticed that some of my views are drawing solid black rather than their patterned background color. The ex

相关标签:
2条回答
  • 2020-12-29 15:24

    Answering my own question (it took me a few days to debug this, so hopefully this saves somebody else some time ;) ):

    The root cause involves using an patterned UIColor (via +[UIColor colorWithPatternImage:]) as a background color on a UIView that is above a UIImageView with the same image.

    Example:

        UIImageView *imageView = [[UIImageView alloc] initWithImage:anImage];
        [_containerView addSubview:imageView];
    
        UIColor *patternColor = [UIColor colorWithPatternImage:anImage];
        UIView  *patternView = [[UIView alloc] initWithFrame:frame];
        [patternView setBackgroundColor:patternColor];
        [_containerView addSubview:patternView];
    

    Both views draw black, and there appears to be a caching issue where all other uses of the image draws black until the application is suspended/resumed.

    I filed issue #10795514 with Apple to report this, but it looks like it made it into 5.1. A reduction of this problem is available at: http://iccir.com/public/radar/Radar10795514.zip

    The only workaround I found was to flatten the view hierarchy and draw the pattern image twice in the same view.

    0 讨论(0)
  • 2020-12-29 15:31

    I was having this problem with iOS 5.1 on an iPad where I was using colorWithPatternImage on a UIScrollView like this:

    scrollView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"YOURIMAGE.jpg"]];
    

    This works in iOS 6 (e.g. iPad2 and beyond), however, on an original iPad, where you can only update your iOS to 5.1.1, this will show up white or another solid color you defined somewhere. The fix is to use a slightly less attractive method where you set the backgroundView of the scrollView like this:

    scrollView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"YOURIMAGE.png"]];
    

    I've tested this in iOS 6 and iOS 5.1, so it should also apply to the iPhone if you're running into problems there.

    0 讨论(0)
提交回复
热议问题