如何在 UIScrollView 上方添加一个浮动按钮?
How to add a button floating above the UIScrollView?
假设我有一个 UIScrollView 和一个按钮,如何设置按钮浮动在 UIScrollView 或 Window 上方? (或将按钮固定在屏幕中央,无论它如何滚动)。
如有任何建议,我们将不胜感激。
假设您正在使用故事板,只需将按钮添加到滚动视图上方而不是其中。
您可能还想将按钮的约束设置为滚动视图的父视图和按钮,使其完全独立于滚动视图。
所以它会是这样的:
在 ViewDidLoad 方法中:
// 添加滚动视图
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];
假设我有一个 UIScrollView 和一个按钮,如何设置按钮浮动在 UIScrollView 或 Window 上方? (或将按钮固定在屏幕中央,无论它如何滚动)。
如有任何建议,我们将不胜感激。
假设您正在使用故事板,只需将按钮添加到滚动视图上方而不是其中。 您可能还想将按钮的约束设置为滚动视图的父视图和按钮,使其完全独立于滚动视图。
所以它会是这样的:
在 ViewDidLoad 方法中:
// 添加滚动视图
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];