11

在我努力升级我的应用程序以支持 IOS7 时,我发现它UIPageControl不支持UIImageView. 他们改变了它。

我将其子类化UIPageControl以放置自定义圆圈而不是常规圆圈(附上示例)

我的课是:

- (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];
}

PageControl(蓝色为当前页面)

现在在 IOS7 中出现以下错误:

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

经过调查,我了解到以下代码会导致错误:

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

我检查了子视图,发现它是 UIView 而不是 UIImageView。可能苹果改变了一些东西。

知道如何解决吗?

4

5 回答 5

38

看起来他们将子视图更改为标准UIView。我设法通过这样做来解决它:

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;
    }
}
于 2013-09-25T07:08:05.063 回答
1

也许点不是一种 UIImageView,所以试试这样

UIImageView* dot = [self.subviews objectAtIndex:i];
if ([dot isKindOfClass:[UIImageView class]]) {
    if (i == self.currentPage) 
        dot.image = activeImage;
    else 
        dot.image = inactiveImage;
}
于 2013-09-24T08:10:12.387 回答
1

我有一个更清洁的解决方案:

for (int i = 0; i < [self.subviews count]; i++) {
    UIView *dotView = [self.subviews objectAtIndex:i];
    if ([dotView isKindOfClass:[UIImageView class]]) {
        UIImageView* dot = (UIImageView*)dotView;
        dot.frame = CGRectMake(dot.frame.origin.x, dot.frame.origin.y, _activeImage.size.width, _activeImage.size.height);
        if (i == self.currentPage)
            dot.image = _activeImage;
        else
            dot.image = _inactiveImage;
    }
    else {
        dotView.frame = CGRectMake(dotView.frame.origin.x, dotView.frame.origin.y, _activeImage.size.width, _activeImage.size.height);
        if (i == self.currentPage)
            [dotView setBackgroundColor:[UIColor colorWithPatternImage:_activeImage]];
        else
            [dotView setBackgroundColor:[UIColor colorWithPatternImage:_inactiveImage]];
    }
}

这个想法不是将子视图添加到 iOS7 的 UIView,而是设置 UIView 背景图像。

于 2013-12-30T11:50:14.470 回答
1

只需对 devgeek 的解决方案进行一点重构,使其更紧凑

for (int i = 0; i < [self.subviews count]; i++) {
    UIImage *customDotImage = (i == self.currentPage) ? _activeDot : _inactiveDot;
    UIView *dotView = [self.subviews objectAtIndex:i];
    dotView.frame = CGRectMake(dotView.frame.origin.x, dotView.frame.origin.y, customDotImage.size.width, customDotImage.size.height);
    if ([dotView isKindOfClass:[UIImageView class]]) { // in iOS 6, UIPageControl contains UIImageViews
        ((UIImageView *)dotView).image = customDotImage;
    }
    else { // in iOS 7, UIPageControl contains normal UIViews
        dotView.backgroundColor = [UIColor colorWithPatternImage:customDotImage];
    }
}
于 2014-02-14T14:21:03.520 回答
0

只需覆盖layoutSubviews您的子类UIPageControl

- (void) layoutSubviews
{
    [super layoutSubviews];
    for (UIView* dot in self.subviews)
    {
        CGRect f = dot.frame;
        //sets all the dots to be 5x5
        f.size = CGSizeMake(5, 5);
        //need to reposition vertically as the dots get repositioned when selected
        f.origin.y = CGRectGetMidY(self.bounds) - CGRectGetHeight(f)/2;
        dot.frame = f;
        //update the cornerRadius to be sure that they are perfect circles
        dot.layer.cornerRadius = CGRectGetWidth(f)/2;
    }
}
于 2014-12-04T21:25:08.030 回答