Floating UIButton in UIScrollView

I want to implement the following behavior in my application: I have UIScrollView

in my view with a custom contentSize

, and I want to place UIButton

in the bottom left corner to stay there, when I view the view, I want the button to stay exactly where it was with scroll content scroll at the same time.

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

+3


source to share


2 answers


Are you using an interface builder / storyboard? If so, just add scrolling. Then add the button, but don't add the button as a scroll view. Just add it as a sibling. But make sure scrolling is visible 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)

      

Thus, the button is independent of the type of scrolling, its scrolling, or the size of the content. It could very well be placed above the scroll view.



Edit: quick (not syntax checked, etc.) as an 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 might want to have one view of the content in your scroller view, which is then the container for all the rootstocks. But that doesn't affect what I'm trying to explain.)

+10


source


You can add the first button to view. Then scroll View in View and please check xib that the button should not be in the scrollview. Try this, I think it works for you.



0


source







All Articles