我是UisCrollView的新手。我不知道如何添加按钮。在我的应用程序中,我需要一个水平视图中的按钮。但是,当我尝试以编程方式创建UISCrollView时,它不起作用。它正在抛出一些错误。请帮助我
我也尝试使用故事板来实现此功能,但由于如何使用ViewDidload连接它们,但失败了。
这是我到目前为止使用的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.layerPosition = self.toplayer.frame.origin.x;
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Excavations",@"Concrete",@"Framing",@"Insulation" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
}
如果我做错了,请告诉我正确的程序。
谢谢
我现在只能在您的代码中看到一个错误:
[button setTitle:@"Excavations",@"Concrete",@"Framing",@"Insulation" forState:UIControlStateNormal];
settitle:期望只有一个字符串,尝试以下操作:
[button setTitle:@"Excavations" forState:UIControlStateNormal];
您可能想添加此按钮您的滚动视图,因此您需要致电:
[scrollview addSubview:button];
应该有帮助,如果不发布您的错误。
我假设您想通过UicontroleventTouchupinside,而不是uicontroleventtouchdown作为按钮控制事件。
尝试一下。它将工作
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.layerPosition = self.toplayer.frame.origin.x;
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Excavations" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
}