Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a button floating above the UIScrollView?

Let say I have a UIScrollView and a button, how can I set the button floating above the UIScrollView or Window? (or stick the button in the center of the screen no matter how it will scroll).

Any suggestion will be appreciated.

like image 232
Zigii Wong Avatar asked Apr 01 '15 05:04

Zigii Wong


2 Answers

Assuming you are working with a storyboard, simply add the button above the scroll view and not inside it. You might also want to set your constraints of the button to the superview of the scrollview and the button to make it completely independent of the scroll view.

so it will be something like this:

example

like image 172
Aviel Gross Avatar answered Sep 27 '22 23:09

Aviel Gross


At the ViewDidLoad Method:

// Adding 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 

// Adding Button

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];
like image 34
Tanuj Avatar answered Sep 27 '22 22:09

Tanuj