Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fix UIButton on top of UIScrollView

I'm trying to figure out how to overlay a UIButton on top of a UIScrollView. Basically the user would be able to use the scrollview normally. But a button in the top right of the scrollview would remain in a fixed position. Any thoughts?

like image 571
SNV7 Avatar asked May 22 '12 23:05

SNV7


3 Answers

Don't add the UIButton as a subView of the UIScrollView, but instead of "self.view"

Your hierarchy should look like this. Notice how the button is now in the drop down for the scroll view. Just drag the button out

like image 160
shabbirv Avatar answered Nov 06 '22 08:11

shabbirv


In the InterfaceBuilder there will be a Hierarchal list of items you put in your view, drag the button from being a view belonging to the scrollView to belonging to the view containing the subview.

Your view hierarchy will look like the following

mainView

|

----Button

|

----ScrollView

Instead of

mainView

|

----ScrollView

  |

  ----Button

basically It means both your button and your ScrollView will both be subViews of the exact same mainView. If your scrollView is the mainView put an empty UIView above the scrollView and insert the UIButton & UIScrollView into that

like image 25
CStreel Avatar answered Nov 06 '22 09:11

CStreel


IB is a little finicky when dropping new views directly on the 'fake' interface. What you will need to do is go over to the left in the list of views and select your button view. Now drag it up to below the 'self.view' or whatever is your top level view.

YOu should notice as you raise it up and down in the list that a light blue line will appear. The width corresponds to 'subview' or 'parent' view connection. Play with it a bit to see the difference.

When you feel comfortable, you should be able to place the Button in the 'self.view', not as a subview of the scroll view. Additionally, if you want it to appear on TOP (physical 'Z level') you will need it to be below the scroll view in the list. ( this points out a subtle problem with CStreet's solution)

like image 2
MobileVet Avatar answered Nov 06 '22 08:11

MobileVet