how to slide screen in iphone app to look the data which we are entering in textfield [duplicate] - iphone

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making the view slide up to make room for the keyboard?
Xcode/iOS5: Move UIView up, when keyboard appears
As we normally enter data in textfield keyboard appears and it hides the data what we are entering in fields so is there any way that screen should slide up so that we can see data which is entered in the fiedl.

try this code.......
-(void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3]; // if you want to slide up the view
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= moveKeyboard;
}
else
{
rect.origin.y += moveKeyboard;
}
self.view.frame = rect;
[UIView commitAnimations];
}
-(void)keyboardWillShow
{
// Animate the current view out of the way
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
-(void)keyboardWillHide
{
if (self.view.frame.origin.y >= 0)
{
[self setViewMovedUp:YES];
}
else if (self.view.frame.origin.y < 0)
{
[self setViewMovedUp:NO];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
Edit: moveKeyboard is float. Set its value according to your need.

There are notifications (UIKeyboard[Will|Did][Show|Hide]Notification) that tell you the keyboard is about to appear or disappear, and you can use those to trigger the code that moves your views. There are different ways to move the views -- you can move them yourself, adjusting their positions as you like; you can put them all inside a single view so that you only have to move the container; or you can embed them in a scroll view and simply adjust the content offset for the scroll view.
See Apple's document Managing the Keyboard, and in particular the section called Moving Content That is Located Under the Keyboard. There's sample code there, too, and it works quite well.

Related

Turn off default rotate animation in iOS

When I rotate my iDevice form portrait to landscape, the screen rotates fine, but I'm seeing black borders moving with it, so it looks more 'real'. I find it embarrassing to see in iOS 7 and many apps have thrashed this behaviour (like Instagram).
What I want to do is hide those black borders that look totally unnecessary when rotating a device. How do I disable this standard animation?
In the parent view controller viewdidload method add this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Then add this method
- (void) didRotate:(NSNotification *)notification {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIInterfaceOrientationIsLandscape(orientation) && !self.modalViewController) {
[self presentModalViewController:carouselView animated:YES];
[Globals sharedGlobals].startedAtLandscape = YES;
}
if (UIInterfaceOrientationIsPortrait(orientation) && self.modalViewController) {
[self dismissModalViewControllerAnimated:YES];
[Globals sharedGlobals].startedAtLandscape = NO;
}
}
Then to prevent animation:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
return NO;
}
return YES;
}
I found the best solution is turn animation off before rotation, and turn it back after rotation.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
...
[UIView setAnimationsEnabled:NO];
}
- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
...
[UIView setAnimationsEnabled:YES];
}

IPhones- Have the keyboard slide out with the view

We have a view open with keyboard shown , but when the back button clicked , the view slide out from right while the keyboard will slide only when the view disappeared.
if we call resignFirstResponder at viewwilldisappear, the view slide to the right while keyboard slide down at same time.
Is it possible to let the keyboard slide out with the view?
I have tested this and it works in iOS 5.1, however, I don't think this is recommended behaviour.
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows])
if ([[keyboardWindow description] hasPrefix:#"<UITextEffectsWindow"]) {
NSLog(#"%#", [keyboardWindow description]);
[UIWindow beginAnimations:#"fadeKeyboard" context:nil];
keyboardWindow.frame = CGRectMake(keyboardWindow.frame.origin.x + keyboardWindow.frame.size.width, keyboardWindow.frame.origin.y, keyboardWindow.frame.size.width, keyboardWindow.frame.size.height);
[UIWindow commitAnimations];
}
You can also use a notification UIKeyboardWillHideNotification to detect when the keyboard is going to hide, or just use the above code manually.
There is no standard way to do what you want, but...
Basically, keyboard is just a view, presented in it's own UIWindow on top of all your other windows.
So, theoretically, what you need to do is to find keyboard view and move it in desired direction. I think you should use transform property and don't mess up with frame.
Class keyboardClass = NSClassFromString(#"UIPeripheralHostView");
for ( UIWindow *window in [[UIApplication sharedApplication] windows] ) {
for ( UIView *subview in window.subviews ) {
if ( [subview isKindOfClass:keyboardClass] ) {
// that's keyboard
}
}
}
Edited:
If you're talking about UINavigationController and it's default slide animations during push / pop, then, you just need to invoke resignFirstResponder in viewDidDisappear and becomeFirstResponder in viewWillAppear on your text view. That way your keyboard will slide along with your view.
Try sticking resignFirstresponder in the viewDidDisappear method instead.
**Set notificatins and use these methods.....Hope it solve problem:
First of all set your whole view in scrollView**
-(void)keyboardDidHide:(NSNotification *)notif
{
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
scrollView.contentSize=CGSizeMake(320,scrollOriginalFrame.size.height);
}];
keyboardVisible=NO;
}
-(void)keyboardDidShow:(NSNotification *)notif
{
scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+235);
NSTimeInterval duration = 0.4;
[UIView animateWithDuration:duration animations:
^{
[scrollView setContentOffset:CGPointMake(0,162) animated:YES];
}];
keyboardVisible=YES;
}
**In viewDidLoad() add this**
//keyboard
scrollOriginalFrame=self.view.frame;
scrollOriginalFrame.size.height-=103;
scrollView.contentSize=scrollOriginalFrame.size;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil];
keyboardVisible=NO;

how to add done button in keypad

i need to add button done on keypad.
Apple does n't provide such felicity but some of application i found that done ,next,previous buttons.
like this.
how can i add these and how can i give click event to them.
can any one please help me.
1.Define the done button (= return key):
textField.returnKeyType = UIReturnKeyDone;
2.Add the action-listener:
[textField addTarget:self action:#selector(textFieldDoneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
3.Define the action-event:
- (IBAction)textFieldDoneEditing:(id)sender {
[sender resignFirstResponder];
}
Have fun!
EDIT:
Here you can find detailed instructions how to add a Toolbar with Next & Previous above UITextField Keyboard:
http://www.randomsequence.com/articles/adding-a-toolbar-with-next-previous-above-uitextfield-keyboard-iphone/
EDIT2:
Now, I have a really great example for you: "This view extends UITextView adding on top of the keyboard associated with this UITextView a toolbar with a « Done » Button"
I check the code and it is a lot of easier than the first example:
http://blog.demay-fr.net/2009/07/cocoa-how-to-add-a-toolbar-with-button-on-top-of-a-uitextview-in-order-to-add-a-dismiss-button/
EDIT3:
Hmmm, no, I doesn't test to code. But I will test it now!
1.Problem: the right initialization. If I add the UITextView in IB, initWithCoder gets called:
- (id)init {
NSLog(#"init");
if (self = [super init]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (id)initWithCoder:(NSCoder *)decoder {
NSLog(#"initWithCoder");
if (self = [super initWithCoder:decoder]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
NSLog(#"initWithFrame");
if (self = [super initWithFrame:frame]) {
//register a specific method on keyboard appearence
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
return self;
}
2.Problem: There's no view with the the Prefix "UIKeyboard":
for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) {
NSLog(#"keyboardWindow = %#", keyboardWindow);
for (UIView *keyboard in [keyboardWindow subviews]) {
NSLog(#"keyboard = %#", keyboard);
if([[keyboard description] hasPrefix:#"<UIKeyboard"] == YES) {
// THERE'S NO VIEW 'UIKeyboard'!!!
}
}
}
The code doesn't work, I'm sorry... I don't know why there's no view "UIKeyboard"... Maybe the first example will help you at this point and you can build your own solution.

Question about passing a NSNotification for scrolling a view when keyboard shows

I have a view with five UITextFields spaced from top to bottom. When you click in a text field the keyboard pops up and covers the bottom two text fields. I have tweaked the code to the point where if you click in one of the bottom two text fields the view will scroll up so that text field is visible.
The problem I am having is when you start in the first text field and tab through the text fields the view does not scroll when you get to the last two text field. I have determined this is because when tabbing between views my method keyboardWillShow never gets called again after we have clicked in the first text Field.
I have tried using the textFieldDidBeginEditing method to determine which text field now has focus and then I wanted to call my keyboardWillShow method again. But that method takes a NSNotification as an argument and through all of my search I have seen that you never really want to create an NSNotification object and instead want to use postNotificationName from NSNotificationCenter to pass a NSNotification object. I have been unable to get this to work properly.
Here's the relevant code I have.
- (void) viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window];
[super viewWillAppear:animated];
}
- (void) viewWillDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[super viewWillDisappear:animated];
}
- (void) keyboardWillShow: (NSNotification *)notif
{
// get our app delegate so we can access currentTextField
ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.notif = notif;
NSDictionary *info = [notif userInfo];
NSValue *aValue = [info objectForKey: UIKeyboardBoundsUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
float bottomPoint = (appDelegate.currentTextField.frame.origin.y+ appDelegate.currentTextField.frame.size.height+10);
scrollAmount = keyboardSize.height - (self.view.frame.size.height - bottomPoint);
if (scrollAmount > 0)
{
moveViewUp = YES;
[self scrollTheView:YES];
}
else
{
moveViewUp = NO;
}
}
- (void)scrollTheView: (BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if (movedUp)
{
rect.origin.y -= scrollAmount;
}
else
{
rect.origin.y += scrollAmount;
}
self.view.frame = rect;
[UIView commitAnimations];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
// declare app delegate so we can access a varibale in it.
ScrollingAppDelegate *appDelegate = (ScrollingAppDelegate *)[[UIApplication sharedApplication] delegate];
// set the app delegate variable currentTextField to the textField which just got focus so we can access it
// in our other method keyboardWillShow
appDelegate.currentTextField = textField;
// some how call keyboardWillShow here so the view will scroll to the current text field
}
If I am going about this all wrong please let me know. I have been searching the net for answers but the have eluded me so far. Everything I find about scrolling the view only handles one text field and not multiple the way I need it.
Thanks
I didn't bookmark the original post I found this in, but here's a link to the code snipet I've been using for exactly this kind of thing:
https://gist.github.com/295089

Display keyboard without animation

Looked intoUIKeyboardAnimationDurationUserInfoKey but I just can't find anywhere how to set it to a custom value.
UIKeyboardAnimationDurationUserInfoKey is a const string identifier for the dictionary key that holds the animation duration, so there is no way to change it easily.
One way to make the keyboard appear without animation is to observe the keyboard notifications and disable animation when it's about to appear and then reenable them. This, of course, disables any other animation as well.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(willShowKeyboard:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(didShowKeyboard:)
name:UIKeyboardDidShowNotification
object:nil];
- (void)willShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:NO];
}
- (void)didShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:YES];
}
and then then the same for UIKeyboardWillHideNotification/UIKeyboardDidHideNotification notifications.
Try
[UIView performWithoutAnimation:^{
[textField becomeFirstResponder];
}];
iOS8-compatible:
Add the appropriate delegate method:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
[UIView setAnimationsEnabled:NO];
}
or
- (void)textViewDidBeginEditing:(UITextView *)textView {
[UIView setAnimationsEnabled:NO];
}
Add the keyboard notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didShowKeyboard:) name:UIKeyboardDidShowNotification object:nil];
And method:
- (void)didShowKeyboard:(NSNotification *)notification {
[UIView setAnimationsEnabled:YES];
}
I've found the best solution is using UIView.setAnimationsEnabled(_ enabled: Bool).
Swift 3
UIView.setAnimationsEnabled(false)
textField.becomeFirstResponder()
// or textField.resignFirstResponder() if you want to dismiss the keyboard
UIView.setAnimationsEnabled(true)
The answer of #Vadoff works perfect. Here for Swift 3:
override func viewDidLoad() {
super.viewDidLoad()
//...
// Add observer to notificationCenter so that the method didShowKeyboard(_:) is called when the keyboard did show.
NotificationCenter.default.addObserver(self, selector: #selector(type(of: self).didShowKeyboard(_:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
// Make textField the first responder.
textField.becomeFirstResponder() // <- Change textField to the name of your textField.
}
func textFieldDidBeginEditing(_ textField: UITextField) {
// Disable animations.
UIView.setAnimationsEnabled(false)
}
func didShowKeyboard(_ notification: Notification) {
// Enable animations.
UIView.setAnimationsEnabled(true)
}
I had to disable Animations in textViewShouldBeginEditing, textFieldDidBeginEditing did not work for me (iOS 8)
It is pretty simple guys. Do not use UIView:SetAnimationEnabled as that will be potentially troublesome. This is how i remove animation when keyboard show.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[txtFirstName becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[txtFirstName resignFirstResponder];
}