有没有办法改变页面指示器点的颜色

发布于 2024-09-12 12:16:44 字数 158 浏览 12 评论 0原文

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

提前致谢

I am newbie to iphone programming, I am trying to develop an app which uses page control. My view's background color is white and page controllers default one is also white, which makes page control invisible on my view so I have changed the back ground color of page control to make is visible. Now, the view appears patched and bad. Is there a way to change just the dots color for page control?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

分开我的手 2024-09-19 12:16:44

我们自定义了 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];
}

然后在视图控制器中我们就像普通的 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];
}

We customized the UIPageControl to use a custom image for the page indicator, I have listed the guts of the class below...

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

Then in the View Controller we just use it like a normal UIPageControl

IBOutlet GrayPageControl* PageIndicator;

Edit:

In the view controller that has the GrayPageControl I have an IBAction that is linked to the GrayPageControl.ValueChanged event.

-(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];
}
生活了然无味 2024-09-19 12:16:44

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

崩溃原因:

在 iOS 7 中 [self.subViews objectAtIndex: i] 返回 UIView 而不是 UIImageViewsetImage 不是 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 的问题。如果有人找到最佳解决方案,请发表评论。 :)

快乐编码

The application crashes in iOS7. I have a Solution for iOS 7:

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

小苏打饼 2024-09-19 12:16:44

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

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

在此处输入图像描述

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

或者,您也可以尝试使用这两个属性:

  pageIndicatorTintColor  property
  currentPageIndicatorTintColor  property

这很简单。我又读了一遍问题,以确保我没有错。你真的只是想改变颜色不是吗?那么是的。

你仍然被那些法律点所困扰。使用 JWD 技术制作精美的点图。

JWD answer is the way to go. However, if all you want is to just change the color, why not do this:

This technique will only work on iOS6.0 and higher!

enter image description here

Select your UIPageControl go to attribute inspector. Tada.

Or alternatively you can play around with these 2 properties:

  pageIndicatorTintColor  property
  currentPageIndicatorTintColor  property

This is so simple. I read the question again to make sure I am not wrong. You really just want to change the color don't you? Then yes.

You're still stuck with that lawsy dots. Use JWD technique for the awesome dot pictures.

不知在何时 2024-09-19 12:16:44

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

pageControl.pageIndicatorTintColor = [UIColor blackColor];

just one line of code fir your demand. The example set to black color.

pageControl.pageIndicatorTintColor = [UIColor blackColor];
悍妇囚夫 2024-09-19 12:16:44

DDPageControl 是一个很好的替代品。一探究竟!

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

DDPageControl is an excellent replacement. Check it out!

You can also view the archived blog post here.

俯瞰星空 2024-09-19 12:16:44

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

pageControl.pageIndicatorTintColor = [UIColor orangeColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];

You can change the page indicator tint color and the current page indicator tint color with the following code:

pageControl.pageIndicatorTintColor = [UIColor orangeColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
秋凉 2024-09-19 12:16:44

如果你只是想改变点的颜色或自定义它,你可以按照 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]

if you just want to change color of dots or customize it you can make it as recommended by JWD, but in little bit another way:

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

As result you will get something like

enter image description here

Also macros:

#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]
折戟 2024-09-19 12:16:44

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

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];

The best and most siple way to do this is to add the following lines of code in the didFinishLaunchingWithOptions method in the AppDelegate.m

UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
长不大的小祸害 2024-09-19 12:16:44

您可能想看看此解决方案 在另一个 stackoverflow 问题中。

You might wanna have a look on this solution in another stackoverflow question.

如此安好 2024-09-19 12:16:44

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

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

Here is a related answer for using a custom image (if you're flexible and can use an image rather than altering the colors directly).

Customize dot with image of UIPageControl at index 0 of UIPageControl

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文