UIButton inside of a scrollview isn't clickable - ios

I have an app where I have a couple buttons inside of a UIScrollview. My view hierarchy is like this:
-- UIScrollview
-- UIView (content view)
-- UIView
-- UIButton
-- UIView
-- UIButton
-- UIView
-- UIButton
But the UIButton isn't clickable. Is it possible that the scrollview is blocking clicks? I checked for:
covering subviews;
user interaction enabled = true (on all views);
Scrollview Delay touch down = false;
Scrollview can cancel on scroll = false
The content view clips to bounds. Cutting off anything outside of the view.
but I couldn't find anything.Is there an issue with buttons and scrollviews?
Thanks :)

Is it possible that the scrollview is blocking clicks?
Not inherently, no.
Is there an issue with buttons and scrollviews?
In a word: no. Obviously buttons inside scroll views work just fine all the time. You probably use them every day in many apps that you use.
If your buttons are not tappable in a scroll view, that's because of something you are doing wrong. It is not because of anything in the nature of buttons and scroll views.

Can you check if you have overlapped a UIView with the UIButton.
And enableUserInteraction is not required for UIViews, try disabling it.
Also check if you have linked the right UIButton event with the action method.

Related

Zooming on content view inside of scrollview doesn't work

I'm creating a scrollview with content view inside of it. inside of the content view there are buttons.
from this point, i already can zoom and pan on content view. but when the touch happen on a button the scrollview doesn't zoom.
I have just had the same problem and solved it with the following steps :
Create a UIView.
Create a UITapGestureRecognizer
Add the UITapGestureRecognizer to the UIView to handle taps.
Create a UIButton.
Disable User Interaction (isUserInteractionEnabled = false) on the button.
Add the UIButton as a SubView of the UIView.
In the UITapGestureRecognizer tapped selector, handle the UIButton touchesUpInside call.
Add the first UIView into the UIScrollView.
Make sure the UIScrollView delay content touches delaysContentTouches = true
The UIScrollView should be able to now seamlessly zoom and you can respond to your buttons.
You can skip the button altogether if you want and just handle the Tap in the Gesture Recognizer. I had to do it the way above because the buttons were a custom sub classed buttons and had a lot of additional custom rendering and functionality included.

Interacting with screen behind UIScrollview

I have a view where content is supposed to scroll over a few elements. One of which is a button. What I did is place the button and all background elements, and then created a scrollview on top of that.
However, the touch events now (obviously) go to the scrollview, and not to the elements in the back. Is it possible to enable interaction through the empty parts of a scroll view?
Screenshot here:
There is a scrollview on top of e.g. the "next" and "edit" buttons. but I'd like these to be clickable anyways.
Thanks.
I'm going to assume that the content you want to scroll is in a subview. ScrollViews always serve as the first layer of any scrollable UI. Put scrollview in your subview then UIView over your ScrollView and over that add your elements.

Making Multiview Button Tappable

This is currently a UIStackView with a UIImageView and a UILabel.
I want the entire thing to be tappable.
My poor man's solution at the moment is to overlay a UIView on top of the UIStackView and add a UITapGestureRecognizer into the mix. But it sucks imo.
If I add UITapGestureRecognizer to UIStackView, nothing happens because subviews fill up the UIStackView.
What is best practice to turn this into a button?
nothing happens because subviews fill up the UIStackView.
Right, but if you turn off the userInteractionEnabled of those subviews, the touch falls through to the UIStackView and the gesture recognizer attached to the UIStackView will work.

Scroll View doesn't scroll when touching and holding then swiping

I have UIScrollView with other UIView elements inside. My other UIView elements are mostly segmented controls. If I click on a UISegmentedControl and hold for a second and then try to scroll, then no scrolling happens. I can only scroll when my finger touches and swipes immediately. I checked other iOS applications such as mail. The behavior was that you touch and hold on a mail, then it's highlighted, but as soon as finger moves away, the scrolling happens and highlighting is undone. How can I implement this behavior?
The issue was the property of UIScrollView. The property canCancelContentTouches was set to NO. Because of that, touch events were handled by subviews of scroll view and swiping didn't cause scrolling.
You can follow one of these steps:
If you are using UISegmentedControl over you UIScrollView, instead of that, add the UISegmentedControl over your controller's view.
If you want to use UISegmentedControl over your scrollView, then you have to create a custom scrollView by creating a subclass of UIScrollView and use an image view instead of UISegmentedControl adding the labels which can act as the segments. This is because your UISegmentControl itself is a touch handler and it breaks the UIResponder chain. So, the scrolling might face issues during the touch events.
Please let me know if any of these works. Thanks :)

How do I pass delayed scroll gestures from a UIButton inside of a UIScrollView?

I have a UIScrollView that contains several UIButtons. Each button is wired up to take an action when the user inputs a touch up event, so they are able to place their finger on the button and it will not be selected until it is raised. Currently, if I made a swipe gesture to scroll the UIScrollView quickly, then the scroll view moves as expected even if the gesture happens directly over a UIButton. HOWEVER, if I hold my finger down too long on a UIButton (about 1 second), the UIScrollView will no longer recognize the gesture and will not be able to scroll until the finger is lifted up.
I am wondering if their is a way to always have the UIScrollView recognize the scroll gesture? Note that this is not an issue if I touch the UiScrollView in a location without a UIButton - it then scrolls as expected.
It may worth a try to let your UIButton respond to UIControlEventTouchDown (maybe with an empty action). I'm not sure if this will work, but conceptually I think it should let the UIButton capture the touch immediately.
(Also make sure you don't enable delaysContentTouches on your scrollview.)
I found the answer to this here: UIScrollview with UIButtons - how to recreate springboard?
Essentially, I had to extend UIScrollView and override touchesShouldCancelInContentView, having it always return YES.

Resources