How to swipe the keyboard along InteractivePopGestureRecognizer? - xcode

I was wondering how to swipe the ViewController with a visible keyboard?
in iOS 7 I can swipe the ViewController from side to side, but the keyboard stays put.
in short, I would like to get to the following state:
Thanks!

Update:
I can't recommend the original solution. While it performed well (when it performed at all), it was an unreliable hack, and could easily break the pop gesture recognizer.
My colleague Dave Lyon came up with a great solution using iOS 7 view controller transitions and packaged it up into a pod:
https://github.com/cotap/TAPKeyboardPop
Once installed, just import the main file and you should be good to go.
Original:
I'd love to know if there's a better way of doing this, but I was able to achieve the behavior by adding the keyboard's view as a subview of the view controller's main view:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.inputAccessoryView = [UIView new];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillHide:(NSNotification *)note
{
if (self.textView.isFirstResponder) {
UIView *keyboardView = self.textView.inputAccessoryView.superview;
if (keyboardView) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:keyboardView];
});
}
}
}
I've found you can also animate the keyboard with the gesture (via addTarget:action:), but the performance is abysmal and doesn't cleanly animate if the gesture is prematurely canceled.

Related

Move specific UITextField when the Keyboard show

I followed the Apple documentation to move a textfield upwards when the keypad appears.
The code works fine my problem is that I need that one specific textfield is moved towards the other, instead of implementing the code Apple every textfield I select is moved upwards ... How can I do to move a specific textField and not all?
Thank you very much, I insert the following code used
-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification {
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = changePasswordTextField.superview.frame;
bkgndRect.size.height -= kbSize.height;
[scrollView setContentOffset:CGPointMake(0.0, changePasswordTextField.frame.origin.y+kbSize.height) animated:YES];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
[scrollView setContentOffset:CGPointZero animated:YES];
}
You can achieve your functionality by following steps.
Set delegate of your UITextField.
Implement textFieldDidBeginEditing method which will be called when keyboard open for textfield. So you may change frame of textfield in this method as below.
-(void)textFieldDidBeginEditing:(UITextField *)textField{
[textField setFrame:CGRectMake(0.0, textField.frame.origin.y-VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
// VALUE = textfield you want to move upward vertically
}
Now, to handle keyboard hiding event, you can set frame of your textfield to its origin in textFieldDidEndEditing method as below.
- (void)textFieldDidEndEditing:(UITextField *)textField{
[textField setFrame:CGRectMake(0.0, textField.frame.origin.y+VALUE,textField.frame.size.width,textField.frame.size.height) animated:YES];
// VALUE = textfield you want to move downward vertically
}
I hope it may help you.

iOS8: What's going on with moving views during keyboard transitions?

After switching to iOS8, I'm getting weird behavior when I move views during a keyboard transition. Can anyone explain what's going on?
Here's a minimal example to demonstrate the problem. I have a simple view with a UITextField and a UIButton. The function nudgeUp moves the text field and the button up by 10 points. It is triggered either by the buttonPressed callback, or the keyboardWillShow callback.
When I tap the button, the code works as expected: buttonPressed calls nudgeUp and the button and text field jump up by 10 points.
When I tap the text field, keyboardWillShow calls nudgeUp, but the behaviour is very different. The button and text field immediately jump down by 10 points, and then slide back up to their original position as the keyboard shows itself.
Why is this happening? How can I regain control of animations during keyboard presentation in iOS8?
#import "ViewController.h"
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
// Called when the keyboard appears.
[self nudgeUp];
}
- (IBAction)buttonPressed:(id)sender {
[self nudgeUp];
}
- (void)nudgeUp
{
CGRect newTextFieldFrame = self.textField.frame;
newTextFieldFrame.origin.y -= 10;
self.textField.frame = newTextFieldFrame;
CGRect newButtonFrame = self.button.frame;
newButtonFrame.origin.y -= 10;
self.button.frame = newButtonFrame;
}
#end
It's AutoLayout. Something changed in iOS8 and you can't just change frame or center points anymore if you have AutoLayout enabled. You have to create an outlet(s) of your constraint (vertical space) and update it accordingly instead of changing frame position. Constraints are like any other ui control and can have an outlet. Constraint change can be animated.
Example:
[UIView animateWithDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue] delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
self.bottomSpaceConstraint.constant = adjustmentedValue;
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
}];
You should use UIKeyboardDidShowNotification (you're using will version) and everything will work as you expect:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
}
- (void)keyboardDidShow:(NSNotification *)notification
{
// Called when the keyboard finished showing up
[self nudgeUp];
}
The explanation is that with UIKeyboardWillShowNotification you are changing the frames too early. After your changes the system will relayout everything to accomodate the keyboard and your changes won't have any effect.
Also, I recommend you to switch to autolayout and forget about frames.
Try using the UIKeyboardWillShowNotification userInfo to give you the frame of the keyboard. Then move the onscreen elements based on that.

Local notification and Open specific screen

when I get local notification I want to open my specific screen.
currently in my app i have used both navigation controller as well as model view controller so at the time of navigation controller, app is switching any view but when model controller exit . it is not opening the screen.
Plz suggest any solution?
There are two way to launch app when notification comes.
1- app is running in background.then open specific screen like
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
// write this line
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
}
in which controller class you are create notification.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadTable)
name:#"reloadData"
object:nil];
}
- (void)reloadTable
{
// create object of that controller which your want to open.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddToDoViewController *cvc = (AddToDoViewController *)[sb instantiateViewControllerWithIdentifier:#"AddToDo"];
[self presentModalViewController:cvc animated:YES];
}

WebView - User has scrolled

I cannot figure out how to have a WebView in Objective-c detect when a scroll has been made. I have looked at the WebFrameLoadDelegate: and found didChangeLocationWithinPageForFrame: method, but that did seem to work.
You'll want to detect the webview is scrolling by using javascript. If you do a quick google search on "uiwebview javascript" you'll see plenty of examples on how to have javascript run in the uiwebivew. Once you get the javascript to detect the scroll occurring then you have the javascript change window.location to something fake and implement the "webView:shouldStartLoadWithRequest:navigationType:" delegate to execute objective-c code. Return NO from the delegate method to not load the request.
Depends on whether you are using a UIWebView (iOS - Cocoa Touch) or WebView (OS X - Cocoa).
iOS (iOS 5 and later):
UIWebView exposes its UIScrollView, and you can set the scroll view's delegate and then implement the delegate scrollViewDidScroll: delegate method (adding to your class's #interface declaration first, of course; this example is in a UIViewController subclass):
- (void)viewDidLoad {
[super viewDidLoad];
[_webView.scrollView setDelegate:self];
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// do something in response to scroll
}
}
OS X:
Add an observer for the NSViewBoundsDidChangeNotification of the WebView (this example is in an NSWindowController subclass):
- (id)initWithWindowNibName:(NSString *)windowNibName {
self = [super initWithWindowNibName:windowNibName];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(_scrollDetected)
name:NSViewBoundsDidChangeNotification
object:_webView];
}
return self;
}
- (void)_scrollDetected {
// do something in response to scroll
}
On OS X, you can detect it by subscribing to NSScrollViewWillStartLiveScrollNotification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(mySelector:)
name:NSScrollViewWillStartLiveScrollNotification object:nil];
I pass nil as the object parameter because when I get it, it doesn't appear to actually come from the enclosingScrollView on the WebView. And there is no scroll view property on WKWebView in Yosemite. So when handling it, you have to check if it's your web view sending it (being paranoid about type safety):
-(void)handleScroll:(id)sender
{
if ([sender isKindOfClass:[NSNotification class]])
{
NSNotification *notif = (NSNotification *)sender;
if ([notif.object isKindOfClass:[NSView class]])
{
NSView *view = (NSView *)notif.object;
if ([view isDescendantOf:self.webView])
{
//Handle scroll here
}
}
}
}
I have only tried this descendent-checking thing with WebView, so if you're using WKWebView, YMMV.
There are other scroll notifications listed in the NSScrollView documentation.

Multiple custom controls that use mouseMoved in one window

At first I had one window with my custom control. To get it to accept the mouse moved events I simply put in it's awakeFromNib:
Code:
[[self window] makeFirstResponder:self];
[[self window] setAcceptsMouseMovedEvents:YES];
Now I'm doing something with four of them in the same window, and this doesn't work so pretty anymore. First off, I took them out of the control's awakeFromNib and decided I'd use my appController to manage it i.e. [window makeFirstResponder:View]
My question is, how do I manage four of these in the same view if I want each one to respond to mouse moved events? Right now, I've told the window to respond to mouseMoved events but none of the views are responding to mouseMoved.
You will also need to override -acceptsFirstResponder to return YES.
#pragma mark NSResponder Overrides
- (BOOL)acceptsFirstResponder
{
return YES;
}
-mouseMoved events are expensive so I turn off mouse moved events when my control's -mouseExited message is called and I turn it on in -mouseEntered.
- (void)mouseEntered:(NSEvent *)theEvent
{
[[self window] setAcceptsMouseMovedEvents:YES];
[[self window] makeFirstResponder:self];
}
- (void)mouseMoved:(NSEvent *)theEvent
{
...
}
- (void)mouseExited:(NSEvent *)theEvent
{
[[self window] setAcceptsMouseMovedEvents:NO];
}
I quickly tested this in my custom control application. I duplicated the control several times in the nib file and it worked as expected.
You may also need:
- (void)awakeFromNib
{
[[self window] setAcceptsMouseMovedEvents:YES];
[self addTrackingRect:[self bounds] owner:self userData:NULL assumeInside:YES];
}
I don't think the -setAcceptsMouseMovedEvents is necessary, but I'm pretty sure the tracking rect code is. You may also need to experiment with the value of the assumeInside: parameter, but that is documented.

Resources