Dismiss UIPopOverScreen when touching NavigationBar - objective-c

I have this UIPopOverScreen that shows when I press a NavigationBar Button.
The problem I want to dismiss it whenever I touch anything outside that popoverscreen.
It partly works but it doesn't dismiss when touching the navigationbar which has been built into the VC that calls the popoverscreen.
I've tried several options to make it disappear but it just hasn't worked yet.
The best results I've gotten when I set:
self.popOverController.passthroughViews = nil;
It worked as I wanted but by just setting this you have to press the navigationbar twice to make it respond again.
I would love to hear your thoughts about a clean solution.

How about adding a UITapGestureRecognizer to react to a tap on the navigation bar? Save a reference to the active Popover and dismiss it by calling a method when the navigation bar receives a tap.

Related

Using tap gesture to dismiss keyboard, rest of screen no longer works

I have a UITableViewController that contains a UISearchBar in one of its cells. Following examples here, I put a addGestureRecognizer in my viewDidLoad to capture taps outside the searchBar and calls resignFirstResponder on the search bar so the keyboard is dismissed.
However, this seems to be trapping all taps, the other items in the tableView no longer respond.
This is odd, because I have the identical code (cut and pasted) in another screen, a UIViewController, and it works fine there. The user can continue clicking on other objects just fine.
Any ideas? I suspect this is a simple view hierarchy issue?
Ahhh, it seems the first version I wrote shouldn't work either. The key is to enable the tab gesture only when the searchBar is entered, and then disable it again when you exit. This question has all the code:
Cancel out of UISearchBar when user taps on view

UIBarButtonItem sent action works on iOS 6 but not iOS 5

I am going to tear my hair out because I cannot think of a logical reason why this occurs on 5 but not 6. So basically, I have a view presented in modal fashion with a navigation bar and a Cancel button (UIBarButtonItem - no custom anything on it, just a standard button) in the navigation bar. When this Cancel button is tapped, I want the modal view to disappear. This works just fine in iOS 6. But for iOS 5 it refuses to work for 2 out of 3 places I have it in my code. What's odd to me is that it works on one but not the other two.
Using storyboards, I right click the button, click on "selector" under Sent Actions, and drag over to the appropriate IBAction method in my view controller's .h file. The link is successfully confirmed with the little blinking animation in Xcode. When I run the app on iOS 6 (simulator or device doesn't matter), the method is successfully executed upon button tap and my modal view dismissed. However, on iOS 5, the method is never even called (I set breakpoints inside the method to see if they would be hit). I've even tried switching the argument in my IBAction method from id to UIBarButtonItem *. No cigar, though.
I've also tried programmatically (in viewDidLoad) setting the cancel button's action to a selector. I've even set the target to the VC. No cigar again. Here is a screenshots of my current setup:
Please note the IBAction methods.
Is there some magical clause in the documentation that I missed? Something awfully special I need to do in order to get it to work in iOS 5? It sure seems like a bug to me, but I'm still fairly new to this stuff so what do I know.
I am using Xcode 4.5.2 and storyboards, and targeting iOS 5 and iOS 6 for the release.
Your help is appreciated, thank you.
I think there may be problem of using GestureRecognizer please comment that code and try it...
I added a separate UIView that resides under my textfields and button and below the navigation bar/title. I added the tap gesture to that programmatically, and that seems to recognize both my tap and the cancel button's action. I still would like to know why the tap gesture swallows up the UIBarButtonItem's action if the gesture is on the root view. This question helped me figure this out. Thanks.
you can exclude view/control from gesture recognizer using following delegate method.
// UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
// exclude your view from gesture recognizer
if(yourView == touch.view)
{
return NO;
}
//
return YES;
}

presentPopoverFromRect is displaying a popover sideways on rotation

-I have a UIView.
-This UIView has a UIButton that when clicked makes a UIAlertView appear.
-Within this UIAlertView I have another UIButton that when clicked calls buttonClicked:
-Within this buttonClicked: method, I call presentPopoverFromRect with a custom view inside. (hourKeyboard is the custom view)
-(void)buttonClicked:(id)sender
{
if(self.hourKeyboard==nil)
{
self.hourKeyboard = [[HourKeyboardViewController alloc] init];
self.hourKeyboardPopover = [[UIPopoverController alloc] initWithContentViewController:self.hourKeyboard];
}
[self.hourKeyboardPopover presentPopoverFromRect:[sender bounds] inView:sender permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
In normal portrait mode, this works great. The popover spawns just to the right of the button, with the arrow correctly pointing left to the button.
There's 2 problems that arrises:
1) While this popover is visible, when you rotate the screen the popover rotates slightly incorrectly (it doesn't reposition it's own x and y position)
2) If the popover is not being shown. If you rotate the screen, then call "buttonClicked", the popover will appear, however, its being shown sideways above the button with the arrow pointing "down" towards the button (technically left in relation to the sideways popover view). If you dismiss it, rotate the screen, then call "buttonClicked", the popover now appears upside down with the button pointing "right" to the button (again, technically left in relation to the sideways popover view)! Repeat to make it sideways again, then right-side up again.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
My thinking:
1) I believe I can just reposition the x and y, however, I've also read that you should dismiss the popover and present it over again on a rotation. I'll see if I can get the first one working, however I'm more concerned about the second problem.
2) I have no idea how to fix this rotation issue. It seems that when you rotate to landscape without the popover being visible. And then you call presentPopoverFromRect, the popover is created with the iPad thinking it's still in portrait view by mistake. That's the behavior it's giving, however, I'm not sure how to make the iPad not make this mistake.
-=-=-=-=-=-=-=-=-=
Thanks again for any help you can provide!
-=-=-
Slight Update:
1) It was easy to just dismiss the popover from the main view on rotation. And this seems to be the general way everyone deals with this issue.
2) Trying out various things such as changing the frame, using CGAffineTransformMakeRotation, and others...but no luck thus far
-=-=-
Another Update:
2) After a lot of testing, it seems to be a direct issue with UIAlertView. If I place the view within UIAlertView (currently doing), the AlertView doesn't tell the popover that the screen is rotated...thus creating the issue
It looks like the only way to fix this is to drop the UIAlertView completely. Instead of showing the UIAlertView, I'll disable the various background views manually (like Alert View was doing) and then show a custom UIView that looks darn similar to the AlertView. From there, I should be able to show the popover without any issues. I'll let yea know how it turns out.
-=-=-=-=-=-=-=-
Final Solution:
I ended up just creating my own view, and having that view imitate a UIAlertView. Then when I spawned the popover, I placed it in the root view controller. Worked much MUCH better, but required more work since I had to manually create my own View instead of the premade UIAlertView. Either way, apparently UIAlertView fails at telling a UIPopoverover subview what rotation it is in.
dismiss the popover in willRotateToInterfaceOrientation and show it again in didRotateFromInterfaceOrientation.
It works with no problems.
EDIT:
Sorry, I misunderstood your second problem.
If some part of your view hierarchy is displayed with bad orientation, one of your controllers is probably missing shouldAutorotateToInterfaceOrientation method.

Interact with other views while a popover is active

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.

How to suppress virtual Keyboard slide-in animation?

I've got a problem with creating a modal search view that emulates the behaviour of that of the Weather app. Specifically, there are two animations, that are bothering me and introduce unneeded 0.2 s delays:
When the modal view becomes visible, I give focus to the UISearchDisplayController.searchBar by caling becomeFirstResponder in viewDidAppear. However, the keyboard is not visible, when the modal view has slid in, but needs another 0.2s to slide in after the animation of tehe modal view transition is complete. Moving the call to another callback like viewWillAppear or viewDidLoad did no good, the keyboard won't show up in the first place.
When the user touches cancel, there is another animation taking place, before the delegate's searchDisplayControllerDidEndSearch method is called, expanding the search text field and "melting" away the button. Again, this animation is unneded as the modal view is supposed to transition out when the button is touched.
Additionally, when I dismiss and re-present the same view, not only does the keyboard slide in after the transition, but the cancel button does the same (luckily simultaneously).
I am aware of a similar problem described here: Keyboard Animation Issues When Calling becomeFirstResponder within a Modal View Controller.
However, it seems like the behaviour of the search bar is sligtly differet then that of text field. I could not reproduce the steps described by that author to make the keyboard visible by calling becomeFirstResponder in viewDidLoad.
Regards,
Chris
I think I found your answer. When you add a search bar using the interface builder, you can do it two ways: "Search bar" and "Search bar and Search Display Controller".
I was using the second and was having the very same problem you described. I could only invoke the keyboard (using becomeFirstResponder) on "viewDidAppear". But if you do it adding just the search bar it works. Now I can call becomeFirstResponder on "viewDidLoad" and the keyboard appears together with the view itself.
I means a little more work, but really not much. You have to set your controller to be the delegate of the search bar. I added a list view for the results and made my controller become its delegate and its datasource.