如何在ios中自定义页面视图指示器

如何在ios中自定义页面视图指示器,ios,objective-c,Ios,Objective C,我想在我的视图控制器中以以下方式显示页面指示器。对于已访问的页面,我想显示绿色记号\图像 为UIPageControl创建一个自定义类,我们将其命名为“CustomPageConcoller”: CustomPageContoller.h: @interface CustomPageContoller : UIPageControl { UIImage* activeImage; UIImage* inactiveImage; } CustomPageContoller.m文件

我想在我的视图控制器中以以下方式显示页面指示器。对于已访问的页面,我想显示绿色记号\图像

为UIPageControl创建一个自定义类,我们将其命名为“CustomPageConcoller”:

CustomPageContoller.h:

@interface CustomPageContoller : UIPageControl
{
    UIImage* activeImage;
    UIImage* inactiveImage;
}
CustomPageContoller.m文件:

@implementation CustomPageContoller

-(id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];

    activeImage = [UIImage imageNamed:@"active_page_image.png"] ;
    inactiveImage = [UIImage imageNamed:@"inactive_page_image.png"] ;

    return self;
}

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

-(void) setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];
    [self updateDots];
}
现在为UIPageControl添加一个操作:

ViewController.m文件:

#import "CustomPageContoller.h"

@interface PullRefreshViewController : UIViewController{
    IBOutlet CustomPageContoller *PageIndicator;
}
-(IBAction) pageChanged:(id)sender
{
    NSInteger page = PageIndicator.currentPage;

    // update the scroll view to the appropriate page
    CGRect frame = ImagesScroller.frame;
    frame.origin.x = frame.size.width * page;
    frame.origin.y = 0;
    [ImagesScroller scrollRectToVisible:frame animated:YES];
}
在ViewController界面生成器中,将UIPageControl的自定义类设置为CustomPageContoller。并与您的自定义UIPageControl(即PageIndicator)建立IBOutlet连接

不要忘记将活动页面和非活动页面添加到项目中


p.S-积分到

您需要为此创建一些自定义代码。请仔细查看已被接受的答案,并放置您的图像而不是颜色。