UIPageControl 不与 IOS7 中的 UIImageView 一起使用
Posted
技术标签:
【中文标题】UIPageControl 不与 IOS7 中的 UIImageView 一起使用【英文标题】:UIPageControl is not with UIImageView in IOS7 【发布时间】:2013-09-24 07:33:14 【问题描述】:在我努力升级我的应用程序以支持 ios7 的过程中,我发现UIPageControl
不支持UIImageView
。他们已经改变了它。
我将UIPageControl
子类化,以便放置自定义圆圈而不是常规圆圈(附上示例)
我的班级是:
- (id)initWithFrame:(CGRect)frame
// if the super init was successfull the overide begins.
if ((self = [super initWithFrame:frame]))
// allocate two bakground images, one as the active page and the other as the inactive
activeImage = [UIImage imageNamed:@"active_page_image.png"];
inactiveImage = [UIImage imageNamed:@"inactive_page_image.png"];
return self;
// Update the background images to be placed at the right position
-(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;
// overide the setCurrentPage
-(void) setCurrentPage:(NSInteger)page
[super setCurrentPage:page];
[self updateDots];
现在在 IOS7 中出现以下错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setImage:]: unrecognized selector sent to instance 0xe02ef00'
经过调查,我了解到以下代码会导致错误:
UIImageView* dot = [self.subviews objectAtIndex:i];
if (i == self.currentPage) dot.image = activeImage;
else dot.image = inactiveImage;
我检查了子视图,发现它是 UIView 而不是 UIImageView。可能苹果改变了一些东西。
知道怎么解决吗?
【问题讨论】:
如何快速覆盖“setCurrentPage”? 【参考方案1】:看起来他们将子视图更改为标准UIView
s。我设法通过这样做来解决它:
for (int i = 0; i < [self.subviews count]; i++)
UIView* dotView = [self.subviews objectAtIndex:i];
UIImageView* dot = nil;
for (UIView* subview in dotView.subviews)
if ([subview isKindOfClass:[UIImageView class]])
dot = (UIImageView*)subview;
break;
if (dot == nil)
dot = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, dotView.frame.size.width, dotView.frame.size.height)];
[dotView addSubview:dot];
if (i == self.currentPage)
if(self.activeImage)
dot.image = activeImage;
else
if (self.inactiveImage)
dot.image = inactiveImage;
【讨论】:
如何快速覆盖“setCurrentPage”? 尊敬的先生,我想知道您的代码放在哪里? 像个魅力!!【参考方案2】:也许 dot 不是 UIImageView 的一种,所以试试这样
UIImageView* dot = [self.subviews objectAtIndex:i];
if ([dot isKindOfClass:[UIImageView class]])
if (i == self.currentPage)
dot.image = activeImage;
else
dot.image = inactiveImage;
【讨论】:
【参考方案3】:我有一个更清洁的解决方案:
for (int i = 0; i < [self.subviews count]; i++)
UIView *dotView = [self.subviews objectAtIndex:i];
if ([dotView isKindOfClass:[UIImageView class]])
UIImageView* dot = (UIImageView*)dotView;
dot.frame = CGRectMake(dot.frame.origin.x, dot.frame.origin.y, _activeImage.size.width, _activeImage.size.height);
if (i == self.currentPage)
dot.image = _activeImage;
else
dot.image = _inactiveImage;
else
dotView.frame = CGRectMake(dotView.frame.origin.x, dotView.frame.origin.y, _activeImage.size.width, _activeImage.size.height);
if (i == self.currentPage)
[dotView setBackgroundColor:[UIColor colorWithPatternImage:_activeImage]];
else
[dotView setBackgroundColor:[UIColor colorWithPatternImage:_inactiveImage]];
这个想法不是为 iOS7 添加子视图到 UIView 而是设置 UIView 背景图像。
【讨论】:
嗯,这很好用。但是我怎么能用这种方法把它们分开呢?这些点要靠在一起,我正在努力寻找解决方案。也为此答案 +1! 您需要做的就是调整dot.frame。只需设置较大的宽度并将点图像放在中间即可。 如果框架更大,图像将重复填充空间,因此是“图案”部分。因此,您无法将图像居中。除非我错过了什么? 是的。你说的对。我们正在设置背景颜色属性。嗯...您可以尝试将 dotView 背景颜色设置为清除,设置框架并在图像顶部添加 UIImageView。在dotView的中间。 我可以尝试一下,我也不确定为什么,但是在框架上设置更大的宽度似乎并没有将它们“推开”。它们只是重叠,有什么想法吗?顺便说一句,谢谢您的帮助,非常感谢。【参考方案4】:只需对 devgeek 的解决方案进行一点重构,使其更加紧凑
for (int i = 0; i < [self.subviews count]; i++)
UIImage *customDotImage = (i == self.currentPage) ? _activeDot : _inactiveDot;
UIView *dotView = [self.subviews objectAtIndex:i];
dotView.frame = CGRectMake(dotView.frame.origin.x, dotView.frame.origin.y, customDotImage.size.width, customDotImage.size.height);
if ([dotView isKindOfClass:[UIImageView class]]) // in iOS 6, UIPageControl contains UIImageViews
((UIImageView *)dotView).image = customDotImage;
else // in iOS 7, UIPageControl contains normal UIViews
dotView.backgroundColor = [UIColor colorWithPatternImage:customDotImage];
【讨论】:
【参考方案5】:只需在 UIPageControl
的子类中覆盖 layoutSubviews
- (void) layoutSubviews
[super layoutSubviews];
for (UIView* dot in self.subviews)
CGRect f = dot.frame;
//sets all the dots to be 5x5
f.size = CGSizeMake(5, 5);
//need to reposition vertically as the dots get repositioned when selected
f.origin.y = CGRectGetMidY(self.bounds) - CGRectGetHeight(f)/2;
dot.frame = f;
//update the cornerRadius to be sure that they are perfect circles
dot.layer.cornerRadius = CGRectGetWidth(f)/2;
【讨论】:
以上是关于UIPageControl 不与 IOS7 中的 UIImageView 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
UIPageControl 自定义类 - 发现 nil 将图像更改为点
无法更改 UIPageControl 中的 currentPage 属性