28

我是 iphone 编程的新手,我正在尝试开发一个使用页面控制的应用程序。我的视图的背景颜色是白色,页面控制器默认也是白色的,这使得页面控件在我的视图中不可见,因此我更改了页面控件的背景颜色以使其可见。现在,该视图看起来已修补且很糟糕。有没有办法只更改页面控件的点颜色?

提前致谢

4

10 回答 10

52

我们自定义 UIPageControl 以使用自定义图像作为页面指示器,我在下面列出了该类的内容......

GrayPageControl.h

@interface GrayPageControl : UIPageControl
{
    UIImage* activeImage;
    UIImage* inactiveImage;
}

GrayPageControl.m

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

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

    return self;
}

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

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

然后在 View Controller 中我们就像普通的 UIPageControl 一样使用它

IBOutlet GrayPageControl* PageIndicator;

编辑:

在具有 GrayPageControl 的视图控制器中,我有一个链接到 GrayPageControl.ValueChanged 事件的 IBAction。

-(IBAction) pageChanged:(id)sender
{
    int 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];
}
于 2010-08-04T21:09:21.113 回答
18

应用程序在 iOS7 中崩溃。我有一个适用于 iOS 7 的解决方案:

崩溃原因:

在 iOS 7 中[self.subViews objectAtIndex: i]返回UIView而不是UIImageView并且不是应用程序崩溃setImage的属性。UIView我使用以下代码解决了我的问题。

检查子视图是UIView(对于 iOS7)还是UIImageView(对于 iOS6 或更早版本)。如果是,UIView我将UIImageView在该视图上添加为子视图,瞧它的工作,而不是崩溃..!!

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

希望这也能解决 iOS7 的问题。如果 Anypone 找到了最佳解决方案,请发表评论。:)

快乐编码

于 2013-10-01T06:09:06.300 回答
15

JWD的答案是要走的路。但是,如果您只想更改颜色,为什么不这样做:

此技术仅适用于 iOS6.0 及更高版本!

在此处输入图像描述

选择您的 UIPageControl 转到属性检查器。多田。

或者,您也可以使用以下 2 个属性:

  pageIndicatorTintColor  property
  currentPageIndicatorTintColor  property

这很简单。我再次阅读了这个问题,以确保我没有错。你真的只是想改变颜色,不是吗?好的。

你仍然坚持那些法律点。使用 JWD 技术制作精美的点图。

于 2012-10-17T16:55:09.637 回答
7

只需一行代码即可满足您的需求。该示例设置为黑色。

pageControl.pageIndicatorTintColor = [UIColor blackColor];
于 2013-07-11T07:45:52.830 回答
5

DDPageControl是一个很好的替代品。看看这个!

您还可以在此处查看存档的博客文章。

于 2011-11-25T20:16:09.610 回答
4

您可以使用以下代码更改页面指示器色调颜色和当前页面指示器色调颜色:

pageControl.pageIndicatorTintColor = [UIColor orangeColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
于 2015-11-04T15:21:10.440 回答
3

如果您只是想更改点的颜色或对其进行自定义,您可以按照 JWD 的建议进行制作,但采用另一种方式:

#pragma mark - LifeCycle

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

#pragma mark - Private

- (void)updateDots
{
    for (int i = 0; i < [self.subviews count]; i++) {
        UIView* dot = [self.subviews objectAtIndex:i];
        if (i == self.currentPage) {
            dot.backgroundColor = UIColorFromHEX(0x72E7DB);
            dot.layer.cornerRadius = dot.frame.size.height / 2;
        } else {
            dot.backgroundColor = UIColorFromHEX(0xFFFFFF);
            dot.layer.cornerRadius = dot.frame.size.height / 2 - 1;
            dot.layer.borderColor = UIColorFromHEX(0x72E7DB).CGColor;
            dot.layer.borderWidth = 1;
        }
    }
}

结果你会得到类似的东西

在此处输入图像描述

还有宏:

#define UIColorFromHEX(hexValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:1.0]
于 2015-02-26T12:08:01.053 回答
2

最好和最简单的方法是在 AppDelegate.m 的 didFinishLaunchingWithOptions 方法中添加以下代码行

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
于 2016-05-04T10:58:55.800 回答
1

您可能想在另一个stackoverflow question中查看此解决方案

于 2010-12-03T16:36:42.390 回答
1

这是使用自定义图像的相关答案(如果您很灵活并且可以使用图像而不是直接更改颜色)。

在 UIPageControl 的索引 0 处使用 UIPageControl 的图像自定义点

于 2016-12-02T18:08:49.967 回答