UIPageControl is not with UIImageView in IOS7

微信扫一扫,分享到朋友圈

UIPageControl is not with UIImageView in IOS7

In my efforts to upgrade my application to support IOS7 I found out that UIPageControl doesn’t support the UIImageView . They have changed it.

I’m subclassing the UIPageControl in order to put custom circles instead the regular ones (attached an example)

My class is:

- (id)initWithFrame:(CGRect)frame
{
    // if the super init was successfull the overide begins.
    if ((self = [super initWithFrame:frame]))
    {
        // allocate two bakground images, one as the active page and the other as the inactive
        activeImage = [UIImage imageNamed:@"active_page_image.png"];
        inactiveImage = [UIImage imageNamed:@"inactive_page_image.png"];
    }
    return self;
}

// Update the background images to be placed at the right position
-(void) updateDots
{
    for (int i = 0; i < [self.subviews count]; i++)
    {
        UIImageView* dot = [self.subviews objectAtIndex:i];
        if (i == self.currentPage) dot.image = activeImage;
        else dot.image = inactiveImage;
    }
}

// overide the setCurrentPage
-(void) setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];
    [self updateDots];
}

Now in the IOS7 I got the following error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setImage:]: unrecognized selector sent to instance 0xe02ef00'

and after investigating I understood that the following code cause the error:

UIImageView* dot = [self.subviews objectAtIndex:i];
if (i == self.currentPage) dot.image = activeImage;
    else dot.image = inactiveImage;

I checked the subviews and saw that it is UIView instead of UIImageView. probably Apple changed something.

Any idea how to fix it?

It looks like they changed the subviews to standard UIView s. I managed to work around it by doing this:

for (int i = 0; i < [self.subviews count]; i++)
{
    UIView* dotView = [self.subviews objectAtIndex:i];
    UIImageView* dot = nil;

    for (UIView* subview in dotView.subviews)
    {
        if ([subview isKindOfClass:[UIImageView class]])
        {
            dot = (UIImageView*)subview;
            break;
        }
    }

    if (dot == nil)
    {
        dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, dotView.frame.size.width, dotView.frame.size.height)];
        [dotView addSubview:dot];
    }

    if (i == self.currentPage)
    {
        if(self.activeImage)
            dot.image = activeImage;
    }
    else
    {
         if (self.inactiveImage)
             dot.image = inactiveImage;
    }
}

Use Clustering Analysis in Tableau to Uncover the Inherent Patterns in Your Data

上一篇

Julie and Dave talk about the iPhone Xs Max

下一篇

你也可能喜欢

UIPageControl is not with UIImageView in IOS7

长按储存图像,分享给朋友