如何在UIScrollView上方添加一个浮动按钮?

6
假设我有一个UIScrollView和一个按钮,如何将按钮设置为浮动在UIScrollView或窗口上方?(或者无论如何滚动,都将按钮粘贴在屏幕中央。)
非常感谢您的建议。
2个回答

13
假设您正在使用故事板,只需将按钮添加到滚动视图上方而不是其中。您还可能希望将按钮的约束设置为滚动视图的超级视图并使按钮完全独立于滚动视图。
因此,它将类似于以下内容: 示例

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];

根据这个答案,按钮不会浮动在scrollView上,因为您将其添加为scrollview的子视图。您应该将其作为主视图的子视图添加。 [self.view addSubview:button]; - GoGreen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接