Sergey Grishchev
Sergey Grishchev

Reputation: 12051

Floating UIButton on UIScrollView

I want to implement the following behaviour in my app: I have a UIScrollView on my view with customizable contentSize and I want to put a UIButton in its bottom left corner to stay there when I scroll the view I want the button to stay exactly where it was, with the contents of the scroll view scrolling at the same time.

What would be the best way to do this? Thanks!

Upvotes: 3

Views: 1987

Answers (2)

Hermann Klecker
Hermann Klecker

Reputation: 14068

Do you use Interface Builder/Storyboard? If so then just add the scroll view. Then add the Button but do not add the button as subview of the scroll view. Just add it as a sibling. But make sure that the scroll view comes first. The hierarchy should look like this:

\--View  (The underlaying UIView
    \--scrollView (The UIScrollView)
        \-- anyView (Any subviews of the scroll view come here
    \--button (The UIButton)

By doing so the button is independent from the scroll view, its scrolling or content size. It may well be placed over the scroll view.

Edit: A quick one (not syntax checked etc.) as example for a programmatic solution.

UIScrollView *myScroller = [[UIScrollView alloc] initWith...];
UIImageView *someImage = [[UIImageView alloc] initWith...]; //Just a sample subivew. 
UILabel *someLabel = [[UILabel alloc] initWith...]; //Another sample.
UIButton *myButton [[UIButton alloc] initWith...]; 

[self.view addSubView:myScroller];
[myScroller addSubView:someImage];
[self.view addSubView:myButton]; //it is important, that myScroller was added first
[myScroller addSubView:someLable]; //the sequence of addding subviews to the content does not matter much.

(You may want to have a single content view within your scroller view which is then a container for all the subivews. But that does not affect what I try to explain.)

Upvotes: 10

user2064700
user2064700

Reputation: 36

You Can Add First Button on view Then Scroll View on View and please check in xib that button should not be in scrollview try this I think it is work for you.

Upvotes: 0

Related Questions