Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by iphonestatus · Jun 21, 2018 at 09:17 AM · ioslayoutheightkeyboard inputtouchscreenkeyboard

Get the height of ios keyboard according to the unity units

I'm making a layout which has a scroll view and a input field beneath the scroll view covering all of the screen and whenever i touch the input field the keyboard poops up and overlaps the inputfield and scroll view i want to move my layout up by height of the keyboard so my layout exactly pops up the keyboard. I'm using this code but it is not giving me the exact value of the keyboard height in ios. Please find the code attached. Moreover I specifically need it for iOS

I'm getting wierd values so it pushes my keyboard way upto center and with some gap between my layout and keyboard.

 public float m_bottom;
  public RectTransform ViewToMove;
       [SerializeField]bool move;
     
       public void FixedUpdate(){
             if (TouchScreenKeyboard.visible) {                
                 TouchScreenKeyboard.hideInput = true;
                 m_bottom = (TouchScreenKeyboard.area.height / 2) - 85f;
                 Debug.Log ("keyboard height and y" + TouchScreenKeyboard.area.height + " bottom  " + m_bottom);
                 ViewToMove.offsetMin = new Vector2 (0, m_bottom);
                 ViewToMove.offsetMax = new Vector2 (0, 0);
             } else
                 MoveDown ();
         }
         public void MoveDown(){
             ViewToMove.offsetMin = new Vector2 (0,0);
             ViewToMove.offsetMax = new Vector2 (0,0);
             Debug.Log(ViewToMove.offsetMax +"\t"+ ViewToMove.offsetMin);
         }

This is my hierarchy ss

screen-shot-2018-06-21-at-24508-pm.png (6.6 kB)
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image moghes · Jun 21, 2018 at 05:14 PM 0
Share

Try the function in this post: https://forum.unity.com/threads/keyboard-height.291038/

It seems everyone is happy with this solution except me :) please inform if it worked for you.

Here's the main function:

 public int GetAndroid$$anonymous$$eyboardSize()
     {        
         using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
         {
             AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
 
             using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
             {
                 View.Call("getWindowVisibleDisplayFrame", Rct);
                 return Screen.height - Rct.Call<int>("height");
             }
         }
     }

 public void LateUpdate () 
 {
     if (open$$anonymous$$eyboard) {
         if (TouchScreen$$anonymous$$eyboard.visible)
             ResizeChatDialogAccordingTo$$anonymous$$eyboard ();
        }
 }

I am calling the function GetAndroid$$anonymous$$eyboardSize() inside the function: ResizeChatDialogAccordingTo$$anonymous$$eyboard ();

avatar image iphonestatus · Jun 22, 2018 at 07:18 AM 0
Share

@moghes actually i need it for iOS there's nothing related to iOS i believe

avatar image moghes iphonestatus · Jun 23, 2018 at 12:03 AM 0
Share

@iphonestatus I thought of writing a very small ios script and use it as a plugin.

I'll paste the code but not tested yet.

 @implementation $$anonymous$$yPlugin

 static $$anonymous$$yPlugin *_sharedInstance;
 
 +($$anonymous$$yPlugin*) _sharedInstance
 {
     static dispatch_once_t onceToken;
     
     dispatch_once(&onceToken, ^{
         _sharedInstance = [[$$anonymous$$yPlugin alloc] init];
     });
     return _sharedInstance;
 }
 
 -(id)init
 {
     NSLog(@"Init called");
     
     self = [super init];
     if(self)
         [self initHelper];
     
     return self;
 }
 
 -(void) initHelper
 {
     NSLog(@"InitHelper called");
 }
 - (int) $$anonymous$$eyboardSize
 {
     return (info[UI$$anonymous$$eyboardFrameBeginUserInfo$$anonymous$$ey] as! NSValue).cgRectValue.height;
 }
 @end
 
 extern "C"
 {  
     int GetIOS$$anonymous$$eyboardSize()
     {
         [[$$anonymous$$yPlugin _sharedInstance] $$anonymous$$eyboardSize];
     }   
 }

this code have in the .mm file and call the function GetIOS$$anonymous$$eyboardSize() from unity.

Note that you have to add this below code anywhere at the level of your variables.

 #if UNITY_IOS
     [DllImport("__Internal")]
     private static extern int GetIOS$$anonymous$$eyboardSize ();
     #endif

Let me know what you get out of this, as I'll update the post later

Just have concerns about the Objective C code to have some mistakes, any native dev might check and correct in case there's smth there

avatar image iphonestatus moghes · Jun 25, 2018 at 06:44 AM 0
Share

@moghes firstly i would like to thank you for the answer secondly i'll try to use your piece of code and then revert you on the same if it works please do put the solution for both android and ios as answer.

[EDIT] Hey @moghes!! I tried your way of using the plugin but it's giving one error i can't get across it i solved some problems like i have to create a seprate .h file to make it work please find my code below and the problem shared as screen shot. This is the GetIos$$anonymous$$eyboardHeight.h

 #ifndef GetIos$$anonymous$$eyboardHeight_h
 #define GetIos$$anonymous$$eyboardHeight_h    
 #endif /* GetIos$$anonymous$$eyboardHeight_h */    
 #import <Foundation/Foundation.h>    
 @interface $$anonymous$$yPlugin : NSObject
     +(int)getGoodNumber;
 @end

This is the GetIosHeight.mm

 #import "GetIos$$anonymous$$eyboardHeight.h"
 
 
 @implementation $$anonymous$$yPlugin
 
  static $$anonymous$$yPlugin *_sharedInstance;
  
  +($$anonymous$$yPlugin*) _sharedInstance
  {
      static dispatch_once_t onceToken;
      
      dispatch_once(&onceToken, ^{
          _sharedInstance = [[$$anonymous$$yPlugin alloc] init];
      });
      return _sharedInstance;
  }
  
  -(id)init
  {
      NSLog(@"Init called");
      
      self = [super init];
      if(self)
          [self initHelper];
      
      return self;
  }
  
  -(void) initHelper
  {
      NSLog(@"InitHelper called");
  }
  - (int) $$anonymous$$eyboardSize
  {
      return (info[UI$$anonymous$$eyboardFrameBeginUserInfo$$anonymous$$ey] as! NSValue).cgRectValue.height;
  }
  @end
  
  extern "C"
  {  
      int GetIOS$$anonymous$$eyboardSize()
      {
          [[$$anonymous$$yPlugin _sharedInstance] $$anonymous$$eyboardSize];
      }   
  }



Here's an attached image for the reference of the bug in xcode that i'm encountering. alt text

screen-shot-2018-06-25-at-31551-pm.png (22.8 kB)
avatar image hexagonius · Jun 22, 2018 at 06:33 PM 0
Share

could you explain the "weird" values?

avatar image iphonestatus hexagonius · Jun 25, 2018 at 06:42 AM 0
Share

@hexagonius like 512f and that when i shift my input field above the given height there's a huge gap and sometimes it overlaps the keyboard. I just have a container which is stretch and stretch in the canvas and is 85 from bottom. so when i get the keyboard height in float from ios's pixels i divide by 2 and then reduce it by 85f. (TouchScreen$$anonymous$$eyboard.area.height / 2) - 85f.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ddk4113 · Jun 28, 2018 at 09:29 AM

Please check the static keyboard height with input field property "hide mobile input" set to true for different iphone and ipads are

if UNITY_IOS

                switch (Device.generation) {
                 case DeviceGeneration.iPhone6:
                 case DeviceGeneration.iPhone6S:
                 case DeviceGeneration.iPhone7:
                 case DeviceGeneration.iPhone8:
                         keyboardHeight = 243f;
                         break;
                 case DeviceGeneration.iPhone6Plus:
                 case DeviceGeneration.iPhone6SPlus:
                 case DeviceGeneration.iPhone7Plus:
                 case DeviceGeneration.iPhone8Plus:
                         keyboardHeight = 230f;
                         break;
                 case DeviceGeneration.iPhone5S:
                 case DeviceGeneration.iPhoneSE1Gen:
                         keyboardHeight = 285f;
                         break;
                 case DeviceGeneration.iPhoneX:
                         keyboardHeight = 282.5f;
                         break;
                 case DeviceGeneration.iPadAir1:
                 case DeviceGeneration.iPadAir2:
                 case DeviceGeneration.iPadPro10Inch1Gen:
                         keyboardHeight = 213f;
                         break;
                 case DeviceGeneration.iPadPro10Inch2Gen:
                         keyboardHeight = 196f;
                         break;
                 case DeviceGeneration.iPadPro1Gen:
                 case DeviceGeneration.iPadPro2Gen:
                         keyboardHeight = 193f;
                         break;
                 default:
                         keyboardHeight = 243f;
                         break;              
          }
                 #endif


Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image iphonestatus · Jun 28, 2018 at 09:32 AM 0
Share

Works like a charm but what about new device that will be added isn't there any dynamic code?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.SetNextControlName , GUI.FocusControl in IOS. Bug? 1 Answer

TouchScreenKeyboard Issue - iOS (iPad) 0 Answers

Lock keyboard layout iOs 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How can I implement iOS TouchScreenKeyboard 'Next' and 'Go' keys? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges