Here is a question for you:
I've got a UIButton that we'll call bottomButton. I have another UIButton that we'll call topButton. What I want to do is place topButton on top of bottomButton such that when you tap topButton, it's action is executed. The problem that I'm having is that when I tap topButton, its bottomButton's action that is getting executed.
So what I want to know is if it is possible to place a UIButton on another UIButton so that when tapped, only the top UIButton registers the tap. Savvy?
Any input is appreciated!
Yes, it's possible. There are three things to check out:
1) Is topButton actually situated above bottomButton in the view stack? [UIView bringSubviewToFront:] may help here.
2) Is topButton large enough to register the tap? Try making topButton larger than 44x44 and see if that helps.
3) Make sure that topButton is enabled.
EDIT
I just saw your comment about adding topButton as a subview of bottomButton, and that's likely to be part of the problem. You could try adding both bottomButton and topButton to a new UIView, and then add that new UIView where you had previously used bottomButton.
If top button is a subview of bottom it won't get the touch events passed to it because the bottom button's hitTest:withEvent: is called first and it will return YES.
You could try overriding hitTest:withEvent: for the bottom buttom to have it call hitTest on its subviews (normally it doesn't have UIControls in its subviews) and return that subview instead if it has a hit.
Ah, I see now from reading your comments. Since you're adding a button TO another button, you'll need to make the superview intercept the touch event.
Read this post:
How to make a superview intercept button touch events?
I'm not sure about this one but have you tried adding them to the view in different orders? Does that make a difference? Test it out and see what happens!
Why would you add a button to a button?
Try adding them to the same parent but putting topButton over bottomButton.
Related
I would like to put UIImageView that is shown on button click. That works good if I put it on the last place of Storyboard. The problem occurs when I do programatically some animation, and after that I click button for showing UIImageView and it is not on front position of the screen.
You can use bringSubviewToFront: method. Something like this:
[self.view bringSubviewToFront:imageView];
If you are adding and removing subviews, it can happen that the order of your views changes.
One solution is to leave the views where they are and only modify the hidden property or the alpha.
Another solution is to use insertSubviewAtIndex: instead of addSubview:.
Additionally, you can also use bringSubviewToFront: just to make sure.
I'm struggling with this problem, and although there are quite some threads on this issue I've not figured it out yet.
I want to load a button into a subview of a scrollview. If I test this subview alone, the button works fine. Also, when I add the button to the scrollview directly, it works fine. But the two combined I don't get any touch event on the button.
My view hierarchy:
UIScrollView
UIView
UIButton (A)
UIButton (B)
So button B works fine, A doesn't.
I've tried messing around with the attributes like Cancellable Content Touches, but so far no luck. I'm loading the subview from the storyboard with:
ViewVC *viewVC = [self.storyboard instantiateViewControllerWithIdentifier:#"ViewVC"];
From the posts I've read, this should just work, so either I'm missing something vital, or I've messed up some attributes along the way. Should I use a delegate to pass the events, or should this be done automatically?
Give your views some colours, and check if one of them is obscuring the other preventing it from being touched. Maybe you UIView is overlapping your UIButton(B), preventing it from being touched.
How are you sure you're adding it?
Make sure you're calling addSubview: on your UIScrollView with your subview as the parameter?
I have a toolBar and I have setup two UIBarButtonItem on it. Both UIBarButtonItem are containing UIButtons as their customViews.
I activate a popover for their Touch Up Inside event as below,
[popover1 presentPopoverFromBarButtonItem:buttonItem1 permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I have another UIButton named clearFilters inside the main view. (Also this is the view which is containing the above toolBar.) I have declared a method for clearFilters button's Touch Up Inside event.
My problem is,
I can not interact with the clearFilters button while a popover is active. So, I'm looking for a solution to interact with this clearFilters button, while a popover is active.
I tried by adding passthroughViews property for a popover as below and it do not work as I expect.
popover1.passthroughViews = [NSArray arrayWithObject:clearFiltersButton];
What could be the reason. As the documentation has mentioned I can not see any issue.
I expect if the above things are correct, then the Touch Up Inside event of the the clearFilters button's should be fire up.
So, please show me if there is any issue or a necessary way to work on this thing.
I'm working on XCode4 and iOS 4.3.
Thanks.
The UIPopoverController documentation reveals why the other bar buttons can be tapped while the popover is visible:
“When presenting the popover, this method adds the toolbar that owns the button to the popover’s list of passthrough views.”
Try querying and logging the popover’s passthrough views. Does it already have things in it? Perhaps something like this would work?
myPopover.passthroughViews = [myPopover.passthroughViews arrayByAddingObject:clearFilters];
I haven’t tested this code, but it’s worth a try.
I'm trying to determine when a UIBackButton is pressed in a sublayer of a navigation app. Would HitTest be used for that? I've seen reference to HitTest, but not exactly sure what it is and how to code for it. Any help is muchly appreciated. Thanks!!
No. Hit testing is the (recursive) process by which UIKit determines which view receives touch events. You shouldn't need to participate in it or invoke it.
If you're using a UINavigationController, it will do the right thing to transition between view controllers when the back button is pressed. If an individual view controller needs to know when it is transitioning off-screen, it should override the -viewWillDisappear: and -viewDidDisappear:. See the documentation for those methods for more information.
I have a UIButton linked up in IB correctly(I believe). The button fires inconsistently, every time I reload the view to show updated info, the button works sometimes and sometimes does not.It gives no errors. I can't find a pattern to when it works and when it doesn't, the same code is run every time I open the view and it still works when it wants. Besides linking it in IB I have also tried to addTarget in ViewDidLoad and remove the IB connection but it still has the same inconsistency,
[_buttonScreen addTarget:self action:#selector(buttonScreenClicked) forControlEvents:UIControlEventTouchUpInside];
If I add NSLog(#"Clicked"); to buttonScreenClicked I see that the method doesn't always get called, what would cause it to do this, I have made sure that I set:
[_buttonScreen setAlpha:0.1];
[_buttonScreen setHidden:NO];
[_buttonScreen setUserInteractionEnabled:YES];
I have no Image, text, or color in the button, but it still works sometimes.
I'm using AFKPageFlipper on the same view but it still had the same problem before I added AFKPageFlipper, so I don't think its that.
If anyone could point me in any direction to start trouble shooting this problem I would appreciate.
Thanks
I just had the same problem and worked it out. The 5 seconds is the clue.
Somewhere you have a gesture recognizer covering the same space as your button. More specifically you have a gesture recognizer that is eating your Taps but not your LongPresses. If you just tap the button the gesture recognizer runs off with your event; Hold your finger down long enough and the gesture recognizer no longer considers it a tap and the event is passed through to your button.
Instrument your Tap gesture recognizer handlers and the problem should pop out at you.
Make sure you don't have any other UIView descendants overlaying the button (like a transparent UIScrollView) as these will intercept the touch events first.
Also make sure that the containing view (the view with the button in) is correctly sized, by default you can place a view outside the bounds of another view and the clipsToBounds is set to false so you will see it but not be able to interact with it.
Things to try:
Do you have any other actions on the button?
Do you have any other UIViews which could possibly be accepting the key presses (above or below, or un-shown)
Also, please check that you have only one UIViewController instance for this screen. Other issues may arrise because of that.
What happens if you dont set the alpha level?
Do you release the object properly in the dealloc only ?
Hope this helps