iPh*_*mer 28 iphone colors uipagecontrol
我是iphone编程的新手,我正在尝试开发一个使用页面控件的应用程序.我的视图背景颜色为白色,页面控制器默认为白色,这使得页面控件在我的视图中不可见,因此我更改了页面控件的背景颜色使其可见.现在,该视图显示已修补且不良.有没有办法改变页面控制的点颜色?
提前致谢
JWD*_*JWD 52
我们定制了UIPageControl以使用页面指示器的自定义图像,我列出了下面类的内容...
GrayPageControl.h
@interface GrayPageControl : UIPageControl
{
UIImage* activeImage;
UIImage* inactiveImage;
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
然后在View Controller中我们就像普通的UIPageControl一样使用它
IBOutlet GrayPageControl* PageIndicator;
Run Code Online (Sandbox Code Playgroud)
编辑:
在具有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];
}
Run Code Online (Sandbox Code Playgroud)
Sah*_*jan 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;
}
Run Code Online (Sandbox Code Playgroud)
希望这也为iOS7解决你的问题.如果Anypone找到最佳解决方案,请发表评论.:)
快乐的编码
Ano*_*ite 15
JWD答案是要走的路.但是,如果您只想更改颜色,为什么不这样做:
此技术仅适用于iOS6.0及更高版本!
选择您的UIPageControl转到属性检查器.田田.
或者你可以玩这两个属性:
pageIndicatorTintColor property
currentPageIndicatorTintColor property
Run Code Online (Sandbox Code Playgroud)
这很简单.我再次阅读这个问题,以确保我没有错.你真的只想改变颜色吗?好的.
你仍然坚持那些法律点.使用JWD技术获得令人敬畏的点图片.
只需一行代码即可满足您的需求.该示例设置为黑色.
pageControl.pageIndicatorTintColor = [UIColor blackColor];
Run Code Online (Sandbox Code Playgroud)