Skip to main content

How To Use Hide And Show Keyboard And Set View Animations In UITextField In Objective C


KEYBOARD HIDE AND SHOW AND VIEW ANIMATIONS IN TEXT FIELD

ViewController.h

float animatedDistance;

ViewController.m

- (void)textViewDidBeginEditing:(UITextView *)textView
{
     CGRect textFieldRect=[self.view.window convertRect:textView.bounds fromView:textView];
     CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
     CGFloat midline=textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
     CGFloat numerator = midline - viewRect.origin.y - 0.2 * viewRect.size.height;
     CGFloat denominator = (0.8 - 0.2) * viewRect.size.height;
     CGFloat heightFraction=numerator / denominator;

          if (heightFraction < 0.0)
          {
               heightFraction= 0.0;
          }
          else if (heightFraction > 1.0)
          {
               heightFraction = 1.0;
          }

     UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];

          if (orientation == UIInterfaceOrientationPortrait || orientation ==                                                                                         UIInterfaceOrientationPortraitUpsideDown)
             {
                    animatedDistance = floorf(216 * heightFraction);
             }
         else
            {
                   animatedDistance = floorf(140 * heightFraction);
            }

      CGRect viewFrame=self.view.frame;
      viewFrame.origin.y-=animatedDistance;
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationBeginsFromCurrentState:YES];
      [UIView setAnimationDuration:0.3];
      [self.view setFrame:viewFrame];
      [UIView commitAnimations];

         // if ([textView.text isEqualToString:@"Enquiry/Request"])
             {
                  // textView.text =@"";
                  // textView.textColor = [UIColor blackColor]; //optional
         // }

           [textView becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
      CGRect viewFrame = self.view.frame;
      viewFrame.origin.y +=animatedDistance;
      [UIView beginAnimations:nil context:NULL];
      [UIView setAnimationBeginsFromCurrentState:YES];
      [UIView setAnimationDuration:0.3];
      [self.view setFrame:viewFrame];
      [UIView commitAnimations];

         // if ([textView.text isEqualToString:@""]) 
            {
                   // textView.text =@"Enquiry/Request";
                   // textView.textColor = [UIColor darkGrayColor]; //optional
         // }

         [textView resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
        [textField resignFirstResponder];
        return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
     CGRect viewFrame = self.view.frame;
     viewFrame.origin.y +=animatedDistance;
     [UIView beginAnimations:nil context:NULL];
     [UIView setAnimationBeginsFromCurrentState:YES];
     [UIView setAnimationDuration:0.3];
     [self.view setFrame:viewFrame];
     [UIView commitAnimations];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
     CGRect textFieldRect=[self.view.window convertRect:textField.bounds fromView:textField];
     CGRect viewRect=[self.view.window convertRect:self.view.bounds fromView:self.view];
     CGFloat midline=textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
     CGFloat numerator = midline - viewRect.origin.y - 0.2 * viewRect.size.height;
     CGFloat denominator = (0.8 - 0.2) * viewRect.size.height;
     CGFloat heightFraction=numerator / denominator;

         if (heightFraction < 0.0)
         {
                heightFraction= 0.0;
          }
         else if (heightFraction > 1.0)
          {
               heightFraction = 1.0;
}

       UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];

       if (orientation == UIInterfaceOrientationPortrait || orientation ==                                                                                        UIInterfaceOrientationPortraitUpsideDown)
        {
                 animatedDistance = floorf(216 * heightFraction);
        }
        else
        {
                 animatedDistance = floorf(140 * heightFraction);
}

    CGRect viewFrame=self.view.frame;
    viewFrame.origin.y-=animatedDistance;
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3];
    [self.view setFrame:viewFrame];
    [UIView commitAnimations];
}


Comments

Popular Posts

React Native - Text Input

In this chapter, we will show you how to work with  TextInput  elements in React Native. The Home component will import and render inputs. App.js import React from 'react' ; import Inputs from './inputs.js' const App = () => { return ( < Inputs /> ) } export default App Inputs We will define the initial state. After defining the initial state, we will create the  handleEmail  and the  handlePassword  functions. These functions are used for updating state. The  login()  function will just alert the current value of the state. We will also add some other properties to text inputs to disable auto capitalisation, remove the bottom border on Android devices and set a placeholder. inputs.js import React , { Component } from 'react' import { View , Text , TouchableOpacity , TextInput , StyleSheet } from 'react-native' class Inputs extends Component { state = { ...

How to recover data of your Android KeyStore?

These methods can save you by recovering Key Alias and Key Password and KeyStore Password. This dialog becomes trouble to you? You should always keep the keystore file safe as you will not be able to update your previously uploaded APKs on PlayStore. It always need same keystore file for every version releases. But it’s even worse when you have KeyStore file and you forget any credentials shown in above box. But Good thing is you can recover them with certain tricks [Yes, there are always ways]. So let’s get straight to those ways. 1. Check your log files → For  windows  users, Go to windows file explorer C://Users/your PC name/.AndroidStudio1.4 ( your android studio version )\system\log\idea.log.1 ( or any old log number ) Open your log file in Notepad++ or Any text editor, and search for: android.injected.signing and if you are lucky enough then you will start seeing these. Pandroid.injected.signing.store.file = This is  file path where t...

An introduction to Size Classes for Xcode 8

Introduction to Size Classes for Xcode In iOS 8, Apple introduced  size classes , a way to describe any device in any orientation. Size classes rely heavily on auto layout. Until iOS 8, you could escape auto layout. IN iOS8, Apple changed several UIKit classes to depend on size classes. Modal views, popovers, split views, and image assets directly use size classes to determine how to display an image. Identical code to present a popover on an iPad  causes a iPhone to present a modal view. Different Size Classes There are two sizes for size classes:  compact , and  regular . Sometime you’ll hear about any.  Any  is the generic size that works with anything. The default Xcode layout, is  width:any height:any . This layout is for all cases. The Horizontal and vertical dimensions are called  traits , and can be accessed in code from an instance of  UITraitCollection . The  compact  size descr...