Prevent hiding keyboard on presentViewController (showing popup) - ios7

There is a view with UITextView on it. UITextView is the only one possible firstResponder for keyboard. Let's call it "BackView".
There is another view which is PopUp (full screen view with transparent background which fades underlying view). Let's call it "PopUp".
PopUp works fine except when the keyboard is on screen. Presenting PopUp forces keyboard to be hidden and, when the PopUp is dismissed, the keyboard is shown again. Since the PopUp is not covering whole BackView it does not look good.
Is there any way to keep keyboard on BackView while PopUp is shown? (there is no need to use keyboard in popup).
PopUp contains:
full screen view with background set to black colour with alpha (to fade underlaying view)
several images, labels and Close button.
PopUp is shown with:
[self setModalPresentationStyle:UIModalPresentationCurrentContext];
[self presentViewController:vc animated:YES completion:nil];
Found solution:
Add PopUp view as a subview for current window

mb.server answered his own question (one that I had as well). But I just wanted to spell out the code explicitly for the benefit of others:
//instantiate popUpVC, then
popUpVC.view.frame = [UIScreen mainScreen].bounds; //assuming you wish the popover to occupy the entire screen; adjust as needed
UIWindow* currentWindow = [[[UIApplication sharedApplication] windows] lastObject];
[currentWindow addSubview:popUpVC.view];

Show:
if let popupVC = storyboard.instantiateViewControllerWithIdentifier("PopupVC") as? PopupVC
{
var frame = UIScreen.mainScreen().bounds
frame.origin.y = frame.size.height
overlayWindow = UIWindow(frame: frame)
popupVC.overlayWindow = overlayWindow
overlayWindow.windowLevel = UIWindowLevelAlert
overlayWindow.rootViewController = popupVC
overlayWindow.makeKeyAndVisible()
UIView.animateWithDuration(0.3, animations:
{
self.overlayWindow.frame.origin.y = 0
})
}
Hide in PopupVC
if let window = overlayWindow
{
UIView.animateWithDuration(0.3, animations: {
window.frame.origin.y = window.frame.height
}, completion: { (finished) -> Void in
self.overlayWindow = nil
})
}

Related

How to make blur effect when view controller slides out

I've got a program with slide out menu. There's Menu button(BarButtonItem) in the right position. When the view controller loads I'm doing next
_menuBarButton.target = self.revealViewController;
_menuBarButton.action = #selector(revealToggle:);
So, when I click on the this button the View Controller slides out to right and I see another View Controller.
So, I want to make the main view controller blur when it slides out. I've got a code how to do it blur, but I can't implement this code because when I tap on the bat button it runs revealToggle: selector.
I've tried:
1. To set action for bar button. And firstly blur view controller:
- (IBAction)menuBarButtonTapped:(UIBarButtonItem *)sender {
[self setBlurEffect];
_menuBarButton.target = self.revealViewController;
_menuBarButton.action = #selector(revealToggle:);
[self.menuBarButton.target performSelector:#selector(revealToggle:)];
}
But app crashes with "unrecognized selector" (the solutions of this problem doesn
t help too).
I've wanted to use willDisapear method but it doesn't run, because the main view controller doesn't disapear. It just slides out.
So, could you help me with this problem?
P.S. I'll be happy if you propose any other effects for main view controller except blur.
P.P.S. Sorry for many mistakes in question.
I've found a good answer for me. I don't use several ViewControllers to implement menu panel.
Now I'm using another UIView calls menuPanel. It contains tableView(UITableView) and blurView(UIView). The second one is under first one.
You can blur this view in to ways:
From the code using next method.
(void) setBlurEffect {// Add blur view
CGRect boundsView = self.someView.bounds;
UIVisualEffectView *tableViewVisualEffect = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight]];
self.tableViewVisualEffect.frame = boundsView;
self.tableViewVisualEffect.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.tableViewVisualEffect.backgroundColor = [UIColor clearColor];
[UIView transitionWithView:self.view
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations: ^ {
[self.someView addSubview:self.tableViewVisualEffect];
}
completion: nil ];
// Here you can add visual effects to any UIView control.
// Replace custom view with navigation bar in above code to add effects to custom view.
}
Where someView is view you want to do blur(or any other effect)
From the story board with Visual Effect View.(I've chosen)
After all I've set tableView and blurView into menuPanelView as pinned to all sides of the menuPanel with 0 distance.
And the last thing I've done I'm changing the position of the menuPanelView with animation:
[self.menuPanelView setFrame:self.visibleTableViewFramePosition];
[UIView animateWithDuration:0.8
animations:^{
[self.menuPanelView setFrame:self.invisibleTableViewFramePosition];
} completion:nil];
Where invisibleTableViewFramePosition is CGRect variable contains position of the menuPanelView when it has to be invisible. You also need to create visibleTableViewFramePosition.
That's all. Hope it may be helpful for someone=)

GMGridView appears offscreen, but overlaps with onscreen view

I am using a custom controller for transitions (could not use navigation controller due to inherent cycles in project, which would allow the navigation controllers stack to grow unbounded [which I assume would cause memory issues]). I am emulating a navigation controllers sliding animation (when transitioning to new screens) using UIView animateWithDuration:animations:completion:
When a button triggering the transition is pressed, the frame of the new view I want to transition to is set to an offscreen position. In the animation for the transition (UIView animateWithDuration:animations:completion:), the view currently on screen has its frame set to an offscreen position, and the new view is set to an onscreen position.
This is inside my custom controller for transitions:
CGFloat windowWidth = self.mainView.frame.size.width;
CGFloat windowHeight = self.mainView.frame.size.height;
CGRect offScreenLeft = CGRectMake(-1*windowWidth, 0.0, windowWidth, windowHeight);
CGRect onScreen = self.mainView.frame;
CGRect offScreenRight = CGRectMake(windowWidth, 0.0, windowWidth, windowHeight);
if (direction == TransitionDirectionForward)
{
if (dragBackgroundOnscreen){
[self.mainView addSubview:self.backgroundView];
[self.mainView sendSubviewToBack:self.backgroundView];
self.backgroundView.frame = offScreenRight;
}
self.currentViewController.view.frame = offScreenRight;
[UIView animateWithDuration:0.65
animations:^{
oldViewController.view.frame = offScreenLeft;
if (dragBackgroundOffscreen)
self.backgroundView.frame = offScreenLeft;
else if (dragBackgroundOnscreen)
self.backgroundView.frame = onScreen;
self.currentViewController.view.frame = onScreen;
}
completion:^(BOOL finished){
[oldViewController.view removeFromSuperview];
if (dragBackgroundOffscreen)
[self.backgroundView removeFromSuperview];
[oldViewController willMoveToParentViewController:nil];
[oldViewController removeFromParentViewController];
[self.currentViewController didMoveToParentViewController:self];
}];
}
else if (direction == TransitionDirectionBackward)
{
if (dragBackgroundOnscreen){
[self.mainView addSubview:self.backgroundView];
[self.mainView sendSubviewToBack:self.backgroundView];
self.backgroundView.frame = offScreenLeft;
}
self.currentViewController.view.frame = offScreenLeft;
[UIView animateWithDuration:0.65
animations:^{
oldViewController.view.frame = offScreenRight;
if (dragBackgroundOffscreen)
self.backgroundView.frame = offScreenRight;
else if (dragBackgroundOnscreen)
self.backgroundView.frame = onScreen;
self.currentViewController.view.frame = onScreen;
}
completion:^(BOOL finished){
[oldViewController.view removeFromSuperview];
if (dragBackgroundOffscreen)
[self.backgroundView removeFromSuperview];
[oldViewController willMoveToParentViewController:nil];
[oldViewController removeFromParentViewController];
[self.currentViewController didMoveToParentViewController:self];
}];
}
I also want the background (self.backgroundView) to remain static unless moving to a screen that has its own background (i.e. I dont want the background to slide if the new views background is the same background).
I am using TransitionDirectionBackward and TransitionDirectionForward just to differentiate between sliding left and sliding right.
Everything works great, except when transitioning involves a GMGridView. the problem when the Gridviews frame is set to an offscreen frame its really setting its currently selected page's frame to that offscreen frame. Other pages of the gridview are not bounded by this frame, so they can appear on screen even before the transition. I tried setting the frame and bounds property on the GridView's viewcontroller's view, but I can still get a page of the gridview appearing onscreen before the transition animation.
Anyone see a solution to this? I was trying to find a way to clip the view of the GridView during the transition so pages dont appear except for the currently selected page, but havent found anything useful.
UPDATE: I found a possible fix by setting alpha = 0.0 for cells that are visible but shouldnt be (later setting alpha = 1.0 when the transition animation is complete). However, I need to know which cells to do this for. I need a way to access the page that the GMGridView is currently on so I can set the adjacent page's cells to have an alpha of 0.0.
UPDATE: Found a way to get it to work by using myGridView convertPoint:(A cgpoint i found by trial and error to be on the first cell of a page.) fromView:myGridView.window. NOTE: I needed an if/else if to check if i was in lanscape left or landscape right since the window coordinates do not rotate when the device is rotated. with this i was able to get the index of the cell at the point on the screen i had specified and then set the previous page to be transparent until after the transition animation.
I would still like to know if there is a way of "clipping" the gridview so that if the cells could be opaque, but just never displayed....?
I think I over complicated the problem. I was looking for the clipsToBounds method of a UIView (although I could have sworn I tried that before). In any case, its working now!

Changing TabView: Resize TabView & Window

I made an application which have a preferences window with 2 tabs.
The first tab have a lots of prefs settings in it, but the second one is very small...
I'd like that the tabview & the window resize when we switch between these 2 tabs.
I act like that, but it doesn't seems to work, when I switch view the "Networks settings" tab is being reduced and disapear (like if the height was going from origin to 0 with animation).
Here is my code (.m):
- (void)tabView:(NSTabView *)tabView
didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
NSRect frame;
int height;
if ([[tabViewItem identifier] isEqualTo:#"Panel settings"]) {
height = 400;
} else if ([[tabViewItem identifier] isEqualTo:#"Network settings"]) {
height = 200;
}
frame = [[tabView window] frame];
frame.size.height = height;
frame.origin.y += height;
[[tabView window] setFrame:frame display:YES animate:YES];
}
Note that I linked the tab view to delegate.
My window is linked to the NSWindow * PrefWindow referencing outlet.
Thanks for your help!
I still think the problem is with the way your springs and struts are set in IB -- make sure you set them for not only for the tab view itself, but also for the individual objects in the view of the tab view item. I noticed that the default settings for my subviews had them all with the top strut set which made any subviews near the bottom of the view disappear when the window shrunk.

UITableView in Popover scrolls out of bounds when keyboard is opened

I have a popover that contains a UITableView. This UITableView has a cell with a text field in it:
alt text http://cl.ly/1b50a21ca8202d22db1b/content
When the popover opens near the bottom of the screen, and I tap the text field to edit it, the keyboard comes up, and the popover moves up to avoid being covered by the keyboard. But as it moves up, the table view in the popover scrolls up out of bounds:
alt text http://cl.ly/4fe64fbfe9518f20560d/content
I can scroll it back down, but how do I prevent this from happening.
I've come to the conclusion that this is a bug. I have filed a bug report with Apple (rdar://8156616) as well as a report on OpenRadar.
For anyone interested, here is a sample project that demonstrates the issue.
Try disabling scrolling on the table view.
[self.tableView scrollingEnabled:NO];
How are you setting the content view of your pop over controller. Please try editing the auto-resizing mask of the content view and set it from top-left.
Hope this helps.
Thanks,
Madhup
Perhaps you can set the UITableView's contentSize manually? So that there is no excess for it to scroll up.
I had the same problem but when hiding the keyboard and reloading the table view!
There is one solution for this problem! What you have to do is first hide the keyboard and reload the table view or change the table view in method recieving keyboard did hide notification!
At first I called
[textView resignFirstResponder]; or [textField resignFirstResponder];
and then
-(void)keyboardDidHide:(NSNotification *)notif {
//Check some conditions if you want
[tableView reloadData];
}
You need to listen to the keyboard will show notification:
NotificationCenter.default.addObserver(self, selector: #selector(handleKeyboardStatusForPopover(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
Then you have to see if the keyboard frame intersects with the popover and subtract the values added from the keyboard to the popover container frame:
#objc func handleKeyboardStatusForPopover(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if keyboardSize.intersects((self.resultPopOverTableViewController!.view.superview!.superview!.frame).insetBy(dx: 0, dy: 60)) {
let popOverContainer = viewControllerDisplayedbyPopover?.view.superview!.superview!
// cellDisplayingDetailInfo = sourceview of popover
popOverContainer!.frame.origin.y = popOverContainer!.frame.minY - keyboardSize.height + cellDisplayingDetailInfo.frame.maxY
}
}
}

Why doesn't UIView.exclusiveTouch work?

In one of my iPhone projects, I have three views that you can move around by touching and dragging. However, I want to stop the user from moving two views at the same time, by using two fingers. I have therefore tried to experiment with UIView.exclusiveTouch, without any success.
To understand how the property works, I created a brand new project, with the following code in the view controller:
- (void)loadView {
self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIButton* a = [UIButton buttonWithType:UIButtonTypeInfoDark];
[a addTarget:self action:#selector(hej:) forControlEvents:UIControlEventTouchUpInside];
a.center = CGPointMake(50, 50);
a.multipleTouchEnabled = YES;
UIButton* b = [UIButton buttonWithType:UIButtonTypeInfoDark];
[b addTarget:self action:#selector(hej:) forControlEvents:UIControlEventTouchUpInside];
b.center = CGPointMake(200, 50);
b.multipleTouchEnabled = YES;
a.exclusiveTouch = YES;
[self.view addSubview:a];
[self.view addSubview:b];
}
- (void)hej:(id)sender
{
NSLog(#"hej: %#", sender);
}
When running this, hej: gets called, with different senders, when pressing any of the buttons - even though one of them has exclusiveTouch set to YES. I've tried commenting the multipleTouchEnabled-lines, to no avail. Can somebody explain to me what I'm missing here?
Thanks,
Eli
From The iPhone OS Programming Guide:
Restricting event delivery to a single view:
By default, a view’s exclusiveTouch property is set to NO. If you set
the property to YES, you mark the view so that, if it is tracking
touches, it is the only view in the window that is tracking touches.
Other views in the window cannot receive those touches. However, a
view that is marked “exclusive touch” does not receive touches that
are associated with other views in the same window. If a finger
contacts an exclusive-touch view, then that touch is delivered only if
that view is the only view tracking a finger in that window. If a
finger touches a non-exclusive view, then that touch is delivered only
if there is not another finger tracking in an exclusive-touch view.
It states that the exclusive touch property does NOT affect touches outside the frame of the view.
To handle this in the past, I use the main view to track ALL TOUCHES on screen instead of letting each subview track touches. The best way is to do:
if(CGRectContainsPoint(thesubviewIcareAbout.frame, theLocationOfTheTouch)){
//the subview has been touched, do what you want
}
I was encountering an issue like this where taps on my UIButtons were getting passed through to a tap gesture recognizer that I had attached to self.view, even though I was setting isExclusiveTouch to true on my UIButtons. Upon reviewing the materials here so far, I decided to put some code in my tap gesture code that checks if the tap location is contained in any UIButton frame and if that frame is also visible on the screen at the same time. If both of those conditions are true, then the UIButton will already have handled the tap, and the event triggered in my gesture recognizer can then be ignored as a pass through of the event. My logic allows me to loop over all subviews, checking if they are of type UIButton, and then checking if the tap was in that view and the view is visible.
#objc func singleTapped(tap: UITapGestureRecognizer)
{
anyControlsBreakpoint()
let tapPoint = tap.location(in: self.view)
// Prevent taps inside of buttons from passing through to singleTapped logic
for subview in self.view.subviews
{
if subview is UIButton {
if pointIsInFrameAndThatFrameIsVisible(view: subview, point: tapPoint)
{
return // Completely ignores pass through events that were already handled by a UIButton
}
}
}
Below is the code that checks if point was inside a visible button. Note that I hide my buttons by setting their alpha to zero. If you are using the isHidden property, your logic might need to look for that.
func pointIsInFrameAndThatFrameIsVisible(view : UIView, point : CGPoint) -> Bool
{
return view.frame.contains(point) && view.alpha == 1
}