我是 iphone 编程的新手,我正在尝试开发一个使用页面控制的应用程序。我的视图的背景颜色是白色,页面控制器默认也是白色的,这使得页面控件在我的视图中不可见,因此我更改了页面控件的背景颜色以使其可见。现在,该视图看起来已修补且很糟糕。有没有办法只更改页面控件的点颜色?
提前致谢
我是 iphone 编程的新手,我正在尝试开发一个使用页面控制的应用程序。我的视图的背景颜色是白色,页面控制器默认也是白色的,这使得页面控件在我的视图中不可见,因此我更改了页面控件的背景颜色以使其可见。现在,该视图看起来已修补且很糟糕。有没有办法只更改页面控件的点颜色?
提前致谢
我们自定义 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];
}
应用程序在 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 找到了最佳解决方案,请发表评论。:)
快乐编码
JWD的答案是要走的路。但是,如果您只想更改颜色,为什么不这样做:
此技术仅适用于 iOS6.0 及更高版本!
选择您的 UIPageControl 转到属性检查器。多田。
或者,您也可以使用以下 2 个属性:
pageIndicatorTintColor property
currentPageIndicatorTintColor property
这很简单。我再次阅读了这个问题,以确保我没有错。你真的只是想改变颜色,不是吗?好的。
你仍然坚持那些法律点。使用 JWD 技术制作精美的点图。
只需一行代码即可满足您的需求。该示例设置为黑色。
pageControl.pageIndicatorTintColor = [UIColor blackColor];
DDPageControl是一个很好的替代品。看看这个!
您还可以在此处查看存档的博客文章。
您可以使用以下代码更改页面指示器色调颜色和当前页面指示器色调颜色:
pageControl.pageIndicatorTintColor = [UIColor orangeColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
如果您只是想更改点的颜色或对其进行自定义,您可以按照 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]
最好和最简单的方法是在 AppDelegate.m 的 didFinishLaunchingWithOptions 方法中添加以下代码行
UIPageControl *pageControl = [UIPageControl appearance];
pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
pageControl.currentPageIndicatorTintColor = [UIColor blackColor];
pageControl.backgroundColor = [UIColor whiteColor];
您可能想在另一个stackoverflow question中查看此解决方案。
这是使用自定义图像的相关答案(如果您很灵活并且可以使用图像而不是直接更改颜色)。