Home > OS >  Swift - UIButton as overlay on UIScrollView
Swift - UIButton as overlay on UIScrollView

Time:12-06

I have a scroll view which contains different ui components such as text fields, labels etc. The scroll view takes almost the full size of the screen. Logically separated, I want to add/display a button which is not a part of the content of the scroll view.

This leads to the problem that my button doesn't react on my touches. In case, I reduce the size of the scroll view so that the frames don't interfere with the ones from the button, everything works as expected.

I tried to bring the button to the front (view.bringSubviewToFront(infoButton)) but it still doesn't work.

CodePudding user response:

I think you want to implement something like 'Gmail app, a plus button is floating at the bottom right of the screen. add your scroll view to view and add other elements to scroll view:

view.addSubview(yourScrollView)
yourScrollView.addSubview(yourButton)
yourScrollView.addSubview(yourLabel)

after you finished adding scrollView's sub views it's time to add your button to -> View (not scroll view) and everything should work fine as you expected

CodePudding user response:

Issue is solved!

It is a matter of how you ad your sub views to the main view. In my case, I added the info button first to the main view followed by adding the scroll view to the main view.

You need to change the order. First you add the scroll view to the main view and then the button.

  • Related