Become first responder without animation - objective-c

Is there a way for a UITextField to become first responder without the animation of the keyboard? That is, make it such that the keyboard just appears?
Basically, I'm pushing a second UIViewController over the UIViewController that has the UITextField, and when that second view controller gets popped off the stack, I want the UITextField to immediately have first responder status so that when the second view controller gets popped, the user never notices the text field wasn't first responder.
Right now I have it so that when it's popped, the keyboard animates up the screen, but I don't want that to be seen.
Any ideas?

You can use a UIView animation like so
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.0];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[textField becomeFirstResponder]; // <---- Only edit this line
[UIView commitAnimations];
This will cause the keyboard to suddenly appear.
You can do the same but with -resignFirstResponder
Swift ..
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.0)
UIView.setAnimationDelay(0.0)
someTextView.resignFirstResponder()
UIView.commitAnimations()

Just set it as the first responder in -viewDidAppear to make the user never notice the field having lost its status.
-(void)viewWillAppear:(BOOL)animated {
if ([self isViewLoaded] && self.textField)
[self.textField becomeFirstResponder];
[super viewWillAppear:animated];
}
I've put a short sample project up on dropbox using this code, if you'd like it.

I'm, mot sure what iOS version do you use or at what point do you call becomeFirstResponder
Calling [textFieldReference becomeFirstResponder] in viewWillAppear: for iOS5/iOS6 seems to work just the way you wanted it to:
it's being called just before view controller will show it's view (that's if you don't handle view controller hierarchy manually) and as soon as it appears in navigation controller keyboard is already presented and text field has focus.
That said, there is no public way that I heard off that would allow to specify keyboard appearance style.

I don't think this was an option when the question was first asked, but UIView.performWithoutAnimation works appropriately.
UIView.performWithoutAnimation {
self.composeView.focusTextField()
}

Related

With Modal Partial Curl, I want to reverse the animations. How would I do this?

With the default presentation modal partial curl segue from one view to another, I would like to reverse the animations. So that, instead of lifting a page to reveal the second view, a page falls down on top of the first view and reveals the second view. Then, when the user is done with the second view, the page lifts up to reveal the first view again.
How on earth would I do this? Make the animations myself using a custom transition somehow?
What you are after is UIView tradition with animation.
[UIView transitionFromView:youBaseView
toView:modalView
duration:3.0
options:UIViewAnimationOptionTransitionCurlDown
completion:^(BOOL finished) {
}];
This will get you effect you described. I think quickest and easiest way to get this done is custom segue. Just subclass UIStoryboardSegue. This post talks about it in detail : How to create custom modal segue in 4.2 Xcode using storyboard
Then for dismissing modal view do something like:
[UIView transitionFromView:modalView
toView:yourBaseView
duration:3.0
options:UIViewAnimationOptionTransitionCurlDown
completion:^(BOOL finished) {
[self dismissModalViewControllerAnimated:NO];
}];
Hope this will help.

UITableViewController and UITextField keyboard

I have a UITableViewController with a grouped static UITableView. I am defining the cells for my static table view on the storyboard. One of the cells has a textfield in it. When this textfield is called, the keyboard pops up, however, the tableview is not automatically resizing like it normally would on a table view controller. So now the keyboard is partially covering the textfield and I can't scroll up.
My understanding is that when you are using a UITableViewController and a tableview, the viewable area should automatically shrink when the keyboard is called. It does work as intended in other parts of my app, just not with this static table view. Does it not work with static tables? Is there something else I am missing? Is there an easy way to solve this?
Thanks
Answer
It has nothing to do with static cells. They should work.
If your controller is already a UITableViewController, check if you used the method viewWillAppear. If you did, you have to call [super viewWillAppear:YES] to get the 'automatic behavior' to work.
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:YES]; // This line is needed for the 'auto slide up'
// Do other stuff
}
This problem turns up easily because the boilerplate code for the controllers don't come with the viewWillAppear method call and if you define it in your controller, you override it.
Extra Information
Look at this link.
Apple Table View Programming Guide
Note: UITableViewController has new capabilities in iOS 3.0. A
table-view controller supports inline editing of table-view rows; if,
for example, rows have embedded text fields in editing mode, it
scrolls the row being edited above the virtual keyboard that is
displayed.... blah....
The important bit
The UITableViewController class implements the foregoing behavior by
overriding loadView, viewWillAppear:, and other methods inherited from
UIViewController. In your subclass of UITableViewController, you may
also override these methods to acquire specialized behavior. If you do
override these methods, be sure to invoke the superclass
implementation of the method, usually as the first method call, to get
the default behavior.
For Swift
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
}
Pushes the view up if one of the table forms is selected for editing (requires keyboard notification implementation)
- (void) keyboardDidShow:(NSNotification *)aNotification
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
self.view.center = CGPointMake(self.view.center.x, self.view.center.y-moveAmount);
[UIView commitAnimations];
isRaised = [NSNumber numberWithBool:YES];
}
Resizes the table (divides height by 2). Swap this into the keyboard did show method. Also, you can use the keyboard did hide method to undo this stuff.
CGRect temp = CGRectMake(mineTable.frame.origin.x, mineTable.frame.origin.y, mineTable.frame.size.width, mineTable.frame.size.height/2);
mineTable.frame = temp;

Using buttons on different views and classes to trigger view switch from the main view controller

I am using storyboard to create my page.. each with it's own class... from the mainViewController, I manage the view change with a swipe gesture recognizer... So far so good... But I have certain pages that will appear as "popup" when swiping up and to get rid of them, the user clicks on the X to remove the view... the thing is that doing this and releasing the view from superview is giving me a white screen as the switch is not done by the mainViewController, since the view is release by the popup class... I think I need to use some sort of delegation to do this.. but my brain just doesn't want to sink in how to use the delegate thing, even after reading about it...
Since the view switching is done on index 1, i'd figure that if I put those popup on index 2 and release them, the view at index 1 would still be there, but.. no..
... so at the beginning of my swipe gesture function, I start declaring the animation process... then I have a switch...case that check for the gesture being done.. left will set the animation to curlUP and right to Curl down... this is what happens after the switch... I also put myView into a myViewTemp, and add the new view to myView in the switch..case statement..
if (myView.title == #"popup1") {
[myView viewWillAppear:YES];
[myViewTemp viewWillDisappear:NO];
// [myViewTemp.view removeFromSuperview];
[self.view insertSubview:myView.view atIndex:2];
[myViewTemp viewDidDisappear:NO];
[myView viewDidAppear:YES];
} else {
[myView viewWillAppear:YES];
[myViewTemp viewWillDisappear:YES];
[myViewTemp.view removeFromSuperview];
[self.view insertSubview:myView.view atIndex:1];
[myView viewDidDisappear:YES];
[myViewTemp viewDidAppear:YES];
}
[UIView commitAnimations];
}

How can I load a UIView from the same NIB (stems from a UITabController)?

So I have a UITabViewController from which a UIViewController (FirstViewController) is loaded (for the first tab button) and I want a button (calcbutton) to open a UIView (calculateview) from within the same NIB of that initial UIView (bedtime)
So basically I want calculateview to animate in when calcbutton is pressed and eventually dismissed with the other "close" button. I have not yet declared or created the IBAction that would dismiss the second view.
Here's an Xcode Screenshot
I would go with a ModalViewController, but that will require moving your other UIView ( calbulateview) to that controller.
If that is not possible, you could still do it within your own view controller.
Use transitionFromView:toView:duration:options:completion:
Here is a short sample:
[UIView transitionFromView:bedtime
toView:calculateview
duration:1.0
options:UIViewAnimationOptionTransitionCurlUp
completion:^(BOOL finished) {
if (finished) {
// In case you need to do something once
// the animation is completed
}
}];
In options you could specify other types of transition animations.
I believe what you are looking for is a modal view controller.
This should explain what you need:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

Bring to front DatePicker on an UITextField

When an UITextField is firstResponder, I would like to bring to front an UIDatePicker (bottom part of the screen) without the "going down keyboard" (no call to UITextField resignFirstResponder).
The aim is to process like UIKeyboard of UITextField which pop-up on nearly everything when it becomeFirstResponder. modalViewController seems to be fullscreen only.
- showDatePicker:(id)sender {
if([taskName isFirstResponder]) [taskName resignFirstResponder];
[self.view.window addSubview: self.pickerView];
// size up the picker view and compute the start/end frame origin
(...)
[UIView commitAnimations];
}
This example is an animation of keyboard going down, and DatePicker going up, behind and not in front.
Do you know a solution ? A piece of code would be welcome. Thanks in advance.
This is simply done by setting the input view of the text field to the Picker View. Then, on Editing did begin tell the picker view to becomeFirst responder. Like this
textField.inputView = pickerView
then using an IBAction called when the Editing Did Begin
-(IBAction) setPickerViewAsFirstResponder:(id)sender
{
[pickerView becomeFirstResponder];
}
This works perfectly. You'll need to implement code to actually set what the picker view is currently showing to be a string in the text field still.
This definitely can be done... simply implement the method below after setting UIViewController <UITextFieldDelegate> in your .h
Long story short, this overrides the keyboard loading before text editing begins.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,245,0,0)];
[self.view addSubview:pv];
return NO;
}