无法将 UIButton 添加到 UIScrollView
Posted
技术标签:
【中文标题】无法将 UIButton 添加到 UIScrollView【英文标题】:Can't add UIButton to UIScrollView 【发布时间】:2013-04-27 17:14:05 【问题描述】:晚安
虽然我已经阅读了很多关于将 UIButton 添加到 UIScrollView 的相关问题,但它对我不起作用。当我使用 UIImages 而不是 UIButtons 时,它就像一个魅力。我在这里想念什么? (不要介意 if 或 for 循环,所有这些都是正确的,图像的路径也是)
for(int j = 0; j < [subcategorie count]; j++)
NSArray *product = [subcategorie objectAtIndex:(j)];
int posX = 0;
for(int k = 1; k <= [product count]; k++)
if((i-1) == currentCategory )
//NSString *fotoName = [NSString stringWithFormat:@"images/product/%@/%@_%i.png",self.categorieen[(i-1)] ,self.categorieen[(i-1)] , k];
NSString *fotoName = [NSString stringWithFormat:@"%@_%i",self.categorieen[(i-1)] , k];
NSString *currentCategory = [NSString stringWithFormat:@"images/product/%@/", self.categorieen[(i-1)]];
NSString *path = [[NSBundle mainBundle] pathForResource:fotoName ofType:@"png" inDirectory:currentCategory];
UIButton *fotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fotoButton setImage:[UIImage imageNamed:path] forState:UIControlStateNormal];
fotoButton.frame = CGRectMake(posX, 252, 718, 520);
fotoButton.backgroundColor = [UIColor clearColor];
self.scrollview.contentSize = CGSizeMake(posX, 252);
[self.scrollview setBounces:YES];
[self.scrollview setPagingEnabled:YES];
[self.scrollview addSubview:fotoButton];
[self.scrollview bringSubviewToFront:fotoButton];
[self.scrollview setUserInteractionEnabled:YES];
self.scrollview.userInteractionEnabled = YES;
[self addSubview: self.scrollview];
posX += 718;
NSLog(@"Fotolink = %@", fotoName);
干杯
【问题讨论】:
in [fotoButton setImage:[UIImage imageNamed:path] forState:UIControlStateNormal]; UIImage imageNamed 部分不正确,尝试添加这个 [UIImage imageWithData:[NSData dataWithContentsOfFile:path]; 你让我开心!谢谢! 【参考方案1】:[UIImage imageNamed:path]
看起来不正确,
试试[UIImage imageWithData:[NSData dataWithContentsOfFile:path]
UIButton *fotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fotoButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:path] forState:UIControlStateNormal];
【讨论】:
确实是这个问题。 Thilina Hewagama 也这么说。还是谢谢!以上是关于无法将 UIButton 添加到 UIScrollView的主要内容,如果未能解决你的问题,请参考以下文章
将 UIView 作为子视图添加到 UIButton 并将其置于前面无法正常工作
将按钮超级视图控制器添加到另一个视图控制器时,UIButton 目标无法识别