- Home /
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
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 ();
@moghes actually i need it for iOS there's nothing related to iOS i believe
@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
@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.
@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
.
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
Works like a charm but what about new device that will be added isn't there any dynamic code?