我有44个按钮,我正在添加到scrollview中,我试图使用一系列数组来执行此操作。我有文件名的名称字符串数组,图片的Y大小(所有x都是相同的)和其他几个。
我在viewDidLoad中运行该方法的代码是:
for (int i = 0; i < 44; i++) {
[self makeLabelsAndButtons:i];
}
方法makeLabelsAndButtons在这里:
-(void)makeLabelsAndButtons:(NSInteger *)indexPath{
NSString *nameString = [nameArray objectAtIndex:indexPath];
NSString *picString = [picArray objectAtIndex:indexPath];
NSInteger picNumb = [[numbers objectAtIndex:indexPath] integerValue];
NSInteger picXnum = [[picXCoords objectAtIndex:indexPath] integerValue];
Button = [UIButton buttonWithType:UIButtonTypeCustom];
[Button addTarget:self action:@selector(presPressed:)
forControlEvents:UIControlEventTouchUpInside];
Button.frame = CGRectMake(160.0, 240.0, 220.0, picNumb);
Button.center = CGPointMake(picXnum, 200.0);
[Button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[ScrollView addSubview:Button];
[ScrollView insertSubview:Button atIndex:1];
NSLog(@"name: %@, pic:%@, picY:%i", nameString, picString, picNumb);
}
没有添加按钮,但我没有错误。我真的不确定我做错了什么
感谢您的帮助!
编辑:这是我初始化我的scrollview
的方式ScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
ScrollView.showsVerticalScrollIndicator=YES;
ScrollView.scrollEnabled=YES;
ScrollView.bounces = YES;
ScrollView.userInteractionEnabled=YES;
[self.view addSubview:ScrollView];
[self.view insertSubview:ScrollView atIndex:1];
ScrollView.contentSize = CGSizeMake(20000,480);
通过NSLogs系统,我确定按钮没有被添加到视图中,我不知道为什么。
答案 0 :(得分:0)
根据以下内容更改您的代码.....它可能有效
[Button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
将上面的内容替换为
NSString *path = [[NSBundle mainBundle] pathForResource:@"picString" ofType:@"png"];
[Button setBackgroundImage:[[UIImage alloc] initWithContentsOfFile:path] forState: UIControlStateNormal];
答案 1 :(得分:0)
我假设您在控制器上声明了两个属性Button
和ScrollView
。
将属性ScrollView
重命名为scrollView
以提高可读性。
@property (nonatomic, weak) IBOutlet UIScrollView *scrollView;
删除属性Button
,因为您不需要它。
更新您的代码:
Button *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(presPressed:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(160.0, 240.0, 220.0, picNumb);
button.center = CGPointMake(picXnum, 200.0);
[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[self.scrollView addSubview:button];
您无需同时添加子视图并插入它。
可能是您没有分配ScrollView
并且向nil对象发送消息不会给您带来错误。您需要确认self.scrollView不是nil,如果是,则检查它是否在您的视图中正确连接。
答案 2 :(得分:0)
您的scrollView似乎可以水平滚动。
你可以尝试
-(void)addButtonsToScrollView
{
//X Offset for button seperation
CGFloat xOffset = 10.0f;
NSUInteger buttonCount = 0;
//In each iteration buttonFrame would be adjusted to next buttonFrame
//This would be used to find the contentSize of scrollView
CGRect buttonFrame = CGRectMake(xOffset, 240.0, 220.0, 40.0f);
while (buttonCount<44)
{
NSString *nameString = nameArray[buttonCount];
NSString *picString = picArray[buttonCount];
CGFloat picNumb = [numbers[buttonCount] floatValue];
CGFloat picXnum = [picXCoords[buttonCount] floatValue];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:@selector(presPressed:)
forControlEvents:UIControlEventTouchUpInside];
buttonFrame.origin.y = (scrollView.frame.size.height - picNumb)/2;
buttonFrame.size.height = picNumb;
button.frame = buttonFrame;
[button setBackgroundImage:[UIImage imageNamed:picString]
forState: UIControlStateNormal];
[button setTitle:nameString
forState:UIControlStateNormal];
[scrollView addSubview:button];
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
buttonCount++;
}
CGSize contentSize = scrollView.frame.size;
contentSize.width = buttonFrame.origin.x;
scrollView.contentSize = contentSize;
}