![](/img/trans.png)
[英]How to change background color of UIWebBrowserView in UIWebView in iOS8 Objective C
[英]How to set UIPageControl with image in iOS8 Objective c?
我想用图像代替点设置UIPageControl
。 我实现了以下代码,但始终会崩溃。请帮助我。
-(void)updateDots
{
for (int i=0; i<[_pageControl.subviews count]; i++) {
UIImageView *dot = [_pageControl.subviews objectAtIndex:i];
if (i==_pageControl.currentPage)
dot.image = [UIImage imageNamed:@"on.png"];
else
dot.image = [UIImage imageNamed:@"off.png"];
}
}
在iOS8中,由于未捕获的异常'NSInvalidArgumentException',我收到以下错误***终止应用程序,原因:'-[UIView setImage:]:无法识别的选择器发送到实例0x7f9dd9eaabb0'
您不应该假定UIPageControl
将包含UIImageViews
(不包含)。
使用公共API的方法如下:
- (void)updateDots
{
// this only needs to be done one time
// 7x7 image (@1x)
_pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"on.png"]];
_pageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"off.png"]];
}
您正在for循环中获取UIView。 检查点是否为UIImageView Class
然后设置图像。
id object = [_pageControl.subviews objectAtIndex:i];
if([object isKindOfClass:[UIImageView Class]])
{
UIImageView *dot = (UIImageView *)object;
if (i==_pageControl.currentPage)
dot.image = [UIImage imageNamed:@"on.png"];
else
dot.image = [UIImage imageNamed:@"off.png"];
}
希望对您有帮助。
请记住,子视图不一定总是必须为UIImageView
。 例如, UITableViewCell
经常在其中包含封闭视图。 您需要在下面进行更改。
-(void)updateDots
{
for (int i=0; i<[_pageControl.subviews count]; i++) {
if ([[_pageControl.subviews objectAtIndex:i] isKindOfClass:[UIImageView Class]])
{
UIImageView *dot = (UIImageView *)[_pageControl.subviews objectAtIndex:i];
if (i==_pageControl.currentPage)
dot.image = [UIImage imageNamed:@"on.png"];
else
dot.image = [UIImage imageNamed:@"off.png"];
}
}
}
我注意到,在UITableViewCell
,单元格内容始终包含一个封闭的UIView
。 您需要递归地进入该视图以找到所需的UIImageView
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.