不能添加的UIButton到的UIScrollView
问题描述:
Goodevening不能添加的UIButton到的UIScrollView
虽然我读过有关添加一个UIButton到的UIScrollView它并没有为我工作了很多相关的问题。当我使用UIImages而不是UIButtons时,它就像一个魅力。我在这里错过了什么? (不介意,如果公司或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);
}
}
}
干杯
答
[UIImage imageNamed:path]
看起来不正确, 尝试[UIImage imageWithData:[NSData dataWithContentsOfFile:path]
UIButton *fotoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fotoButton setImage:[UIImage imageWithData:[NSData dataWithContentsOfFile:path] forState:UIControlStateNormal];
+0
这确实是错误。 Thilina Hewagama刚才也这么说。不管怎么说,还是要谢谢你! – 2013-04-27 17:36:30
在[fotoButton setImage:[UIImage imageNamed:path] forState:UIControlStateNormal]; UIImage imageNamed部分不正确,请尝试添加此[UIImage imageWithData:[NSData dataWithContentsOfFile:path]; – 2013-04-27 17:18:50
你刚刚度过了我的一天!谢谢! – 2013-04-27 17:30:22