假设我有一个UIScrollView和一个按钮,如何设置浮动在UIScrollView或Window上方的按钮? (或者将按钮放在屏幕中央,无论它如何滚动)。
任何建议都将受到赞赏。
答案 0 :(得分:8)
假设您正在使用故事板,只需在滚动视图上方添加按钮,而不是在其中。 您可能还希望将按钮的约束设置为scrollview的超级视图和按钮,使其完全独立于滚动视图。
所以它会是这样的:
答案 1 :(得分:2)
在ViewDidLoad方法:
//添加ScrollView
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);//You Can Edit this with your requirement
//添加按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];