Swift : View in a UIScrollView is unable to detect touch - ios

So I have A UIButton in a UIView within a UIScrollView within a UIViewController within a UIScrollView.. that is whole lot of layers I know but that was required. But the result I got is that the UIButton cannot be tapped because the touch can't be detected on the button. ScrollViews are working perfectly but the elements in the final layer are not detecting touches.
The Layers are as follow:
UIViewController
~UIScrollView
~~UIViewController
~~~UIScrollView
~~~~UIView
~~~~~UIButton
Kindly tell me how can I detect the touch on the last UIView?

you should debug view hierarchy by clicking debug view hierarchy button
look like this.
So you come to know that your button actually get touch events or it is hidden by other views.
Update :
I am uploading another screenshot
You have to run your project then you will able to see this button above console pane.
You can refer Apple documentation for more details.
Hope this will help :)

To solve your current problem, you need to iterate through all gestures on each super view and call requireGestureRecognizerToFail for relevant gestures on subviews. This way you make sure that the subviews get the touches 'first' and superviews handle only those touches which are ignored or not recognize by subviews for whatever reason. (I know this sounds as ridiculous as having a view hierarchy like UIViewController -> UIScrollView -> UIViewController -> UIScrollView -> UIView -> UIButton)
It seems unlikely that you could not have solved your visual design using either UITableView or UICollectionView. So reconsider!! It will save you a lot of such issues.

Related

UIbutton click issue - Swift

I'm trying to add login button overlap on subview, the touch event for uibutton does not work on lower-half.
Can anyone provide me an idea to achieve design like this, with touch event work on all corner ?
You view set up should look something like:
As Inderjeet mentioned in his comment, "You have to keep button outside the UIView otherwise it is not tap-able. Maintain the position of button according to UIView by using constraints."
Now to answer why, set clip to bounds property of your view to true. You will see, on run time, portion which is not tap-able, is also not visible.

Conflict with a UIPanGestureRecognizer on a UITableView's superview

I'm trying to build something roughly similar to the drawer menu in Apple Maps on iOS.
In this Xcode project I'm attaching a UIPanGestureRecognizer on the VC's view, and as the panning happens, move vertically a UITableView with scrolling disabled.
The issue is every time after the pan ends, the didSelectRow method is called only after a second tap happens somewhere on the UITableView. Of course I'd like it to be called after the first tap.
The funny thing is that the bug does not happen if I enable the table's scrolling, and in the gesture recognizer's delegate have shouldRecognizeSimultaneouslyWith returning true.
Other funny thing is a very similar thing seems to happen in Apple Maps itself, if you try pulling the drawer up with the finger resting on a recent location entry from the list inside the drawer.
Thanks for your help!
I don’t understand well what are your saying.
But I think that the main problem is with “Chain Responder”. When you use PanGestureRecognizerand the UITableView property isScrollEnable = false in the Responder Chain the PanGestureRecognizer it is the first who is called, and the system wait to that fails or the event is not handled, then it is passed to the next in the Responder Chain, who is the UITableView. For that reason, it takes too long to get called the didSelectRow function
I suggest to you create a new UIView and insert in the ViewController in the storyboard o nib and put the UITableVIew outside of that UIView, then link the PanGestureRecognizer to that new UIView. In that way the Responder Chain don’t get in conflict with both, because the system can detect when the drag is in the new UIView and call only to the PanGestureRecognizer and when it is in the UITableView will call to the didSelectRow
Best Regards
Write if it does not resolve

UIButton default tap animation inside UITableViewCell

I have a few UIButtons inside UITableViewCells, and all of them are missing default tap animation, however if I long press - animation works. I've found some solutions, like setting type to .system, showsTouchOnHighlight = true, but none of them have helped. What is the problem here?
It's not a "problem" - it's by design.
When you have an object such as a button in a table view or scroll view or collection view, and you "touch" it, the system needs to know whether you are tapping the button or if you want to touch-and-drag to scroll the view that contains the button.
So, the table view (really, the "scroll view part" of the table view), waits to see if you drag your finger or not before it performs any actions.
If you want the button to respond immediately to a touch, you need to set
delaysContentTouches = false
on the containing scroll view(s).
If you search for uibutton inside uitableviewcell delaysContentTouches you should find plenty of discussion on this topic, and various approaches to change the default behavior.
For this problem you can add extension to UIButton and override some methods.
You can check my answer here

Bring UIButton to front layer

I know there is a way of bringing a subview to the front of the hierarchy. However is there a way of bringing a button that i have placed on a story and calling a function that will make that button the top layer so its not hidden by any other elements that are added that aren't in the story board.
Thanks,
I am late for this question, but not too late to answer it by Swift 3
Swift 3
view.bringSubview(toFront: theButton)
A UIButton is a UIView, so you can call -bringSubviewToFront: on your button instance.
uiview's are displayed in the order they are stacked. One can change this to say were to insert the new uiview, which in your case is a uibutton.
One way of doing that is by creating a IBOUtlet of the uibutton and adding that button instance as a subview/newview above an existing uiview.
CODE:
[self.view insertSubview:<new Uiview> aboveSubview:<existing uiview>];
Also if you are not able to figure out the order of the uiview , then you can check that in xcode
When running the project in debug mode under debug window there is a button(highlighted in below image) Debug View Hierarchy, you need to click that. (highlighted in below image)
After that you will able to see all the views rendering on the screen at current instance, using this feature you will be able to understand were is your new uiview in the uiview stack order(shown below).
Swift
The following works great if the view you are working with is being hidden views that are in the same view.
superview?.bringSubviewToFront(self)
Swift 5
In my case I had to insert my view behind some buttons. By insert it at the zero position it was placed behind the buttons.
self.view.insertSubview(self.mapView!, at: 0)
i have the same problem with you and a friend of mine helped me
i can bring my button to another subview using this function on your view controller where you declare your button.
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.shared.keyWindow?.addSubview(chatButton)
}
button.superview?.bringSubviewToFront(button)

UIView with UIButtons not showing up prior to click or rotate

I've been banging my head with this issue for the last two days. Googled a lot but wasn't able to find the answer yet, so I decided to request some help here. Let's see if I get any luck.
I'm coding a reusable control that consists of an UIView with a variable number of customized UIButtons. I implemented initWithFrame:, initWithCoder: and drawRect: where the buttons (which are built prior to drawing) are actually added to the view. Everything is done programmatically since the UIButton content should be supplied when using the control, so there's no XIB for this UIView.
This UIView, let's call it CustomizableBarButton is then used in an UIViewController, let's call it MyTestViewController with a view on it, let's call it customizableBarButtonView.
MyTestViewController's GUI was set on IB where an UIView was tied to customizableBarButtonView (the class was matching accordingly).
MyTestViewController's is a pretty standard class except for the viewWillAppear: that initializes the buttons and passes them to the CustomizableBarButton along with some other options.
The issue is that everything works perfectly...except for the first time!
I mean, when I run the app on the simulator (I haven't tried it on the iPhone yet but I strongly believe that it's not an hardware issue) the MyTestViewController shows the customizableBarButtonView background but not the buttons. Now when you click on the place where a button should be all the buttons suddenly appear!
I'm puzzled since the CustomizablebarButton drawRect: runs before the strange "click n'appear" effect and the buttons are actually added to the subview.
Another hint that my help: if you don't click on the buttons (so you still got no buttons yet) but rotate the device they will also appear as if by magic!
It is probably something very simple but I'm missing it and I'm going nuts...
Can someone lend a hand on this, please?
Thanks in advance!
You said you're adding the buttons in drawRect:. Don't do that. You need to add the buttons in your init methods.

Resources