subviews of Custom UIPageviewControl return UIView in ios7 but UIImageView in ios6

怎甘沉沦 提交于 2020-01-05 10:30:01

问题


Hi I am working on customPageControl. It is working fine on iOS 6 but app crashes on iOS 7. the detailed Scenario is as follow:

I am using a customPageControl file in my project. For detailed CustomPageControl you can go through this link. In iOS 6 [self.subviews objectAtIndex: 1] returns UIImageView but in iOS 7 it returns UIView. And I am using

UIImageView * imageView = [self.subviews objectAtIndex: 1];
imageView.image = [UIImage <SomeImage>];

in iOS 7 it take imageView as UIView and give exception that unrecoganised Selector sent.

Please give me some direction.


回答1:


I have found a solution for this problem. I know it is not the way but Sure that it works till iOS 8 will be launched in the market.

Reason for Crash:

in iOS 7 [self.subViews objectAtIndex: i] returns UIView Instead of UIImageView and setImage is not the property of UIView and the app crashes. I solve my problem using this following code.

Check Whether the subview is UIView(for iOS7) or UIImageView(for iOS6 or earlier). And If it is UIView I am going to add UIImageView as subview on that view and voila its working and not crash..!!

-(void) updateDots
{
    for (int i = 0; i < [self.subviews count]; i++)
    {
        UIImageView * dot = [self imageViewForSubview:  [self.subviews objectAtIndex: i]];
        if (i == self.currentPage) dot.image = activeImage;
        else dot.image = inactiveImage;
    }
}
 - (UIImageView *) imageViewForSubview: (UIView *) view
{
    UIImageView * dot = nil;
    if ([view isKindOfClass: [UIView class]])
    {
        for (UIView* subview in view.subviews)
        {
            if ([subview isKindOfClass:[UIImageView class]])
            {
                dot = (UIImageView *)subview;
                break;
            }
        }
        if (dot == nil)
        {
            dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, view.frame.size.width, view.frame.size.height)];
            [view addSubview:dot];
        }
    }
    else
    {
        dot = (UIImageView *) view;
    }

    return dot;
}

Hope this solve ur issue too for iOS7. and If Anypone find the optimal solution for that please comment. :)

Happy coding



来源:https://stackoverflow.com/questions/19024167/subviews-of-custom-uipageviewcontrol-return-uiview-in-ios7-but-uiimageview-in-io

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!