Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-7724] iOS: the ability to get the cords for the top of the keyboard when it changes

GitHub Issuen/a
TypeNew Feature
PriorityHigh
StatusClosed
ResolutionFixed
Resolution Date2012-05-24T11:09:49.000+0000
Affected Version/sRelease 1.8.2
Fix Version/sRelease 2.1.0, Sprint 2012-11 API
ComponentsiOS
Labelsapi, qe-testadded
ReporterJon Alter
AssigneeSabil Rahim
Created2012-02-21T14:07:29.000+0000
Updated2012-06-08T12:33:14.000+0000

Description

One should be able to know or detect the height/top of the keyboard. When keyboard is switched between different languages, one should know the height of the keyboard.

Comments

  1. Jon Alter 2012-04-03

    Requesting that we be able to get the coordinates of the top of the keyboard. This is especially important for the iPad. As of iOS 5 you can un-doc the keyboard from the bottom as well as move the keyboard up and down. So simply knowing the height of the keyboard will not help. It would be best if we could have an eventListener so that you can know when the keyboard is moved. It would also be helpful if we could tell if the keyboard was split or not.
  2. Pedro Enrique 2012-04-09

    I would like to submit a pull request for this, but I need to know if what I have is correct, syntax as well as code standards This is the notification in AppModule.m
       -(void)startup
       {
       	WARN_IF_BACKGROUND_THREAD_OBJ;	//NSNotificationCenter is not threadsafe!
       	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShutdown:) name:kTiWillShutdownNotification object:nil];
       	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willShutdownContext:) name:kTiContextShutdownNotification object:nil];
       	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard:) name:UIKeyboardWillHideNotification object:nil];
       	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard:) name:UIKeyboardWillShowNotification object:nil];
       	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard:) name:UIKeyboardDidHideNotification object:nil];
       	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard:) name:UIKeyboardDidShowNotification object:nil];
       	if([TiUtils isIOS5OrGreater] == YES)
       	{
       			[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard:) name:UIKeyboardWillChangeFrameNotification object:nil];
       			[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboard:) name:UIKeyboardDidChangeFrameNotification object:nil];
       	}
       	[super startup];
       }
       
    And this is the event:
       -(void)keyboard:(NSNotification *)notification
       {
       	if ([self _hasListeners:@"keyboard"])
       	{
       		// Get the keyboard information
       		NSDictionary *userInfo = [notification userInfo];
       		NSString *notName = [notification name];
       		// Get the begin and end rect values of the keyboard
       		NSValue* beginValue = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
       		NSValue* endValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
       
       		NSValue* currentValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
       
       		CGRect beginRect = [beginValue CGRectValue];
       		CGRect endRect = [endValue CGRectValue];
       
       		// Store the values of each rect in variables for orientation purposes
       		CGFloat beginWidth = beginRect.size.width;
       		CGFloat beginHeight = beginRect.size.height;
       		CGFloat beginX = beginRect.origin.x;
       		CGFloat beginY = beginRect.origin.y;
       
       		CGFloat endWidth = endRect.size.width;
       		CGFloat endHeight = endRect.size.height;
       		CGFloat endX = endRect.origin.x;
       		CGFloat endY = endRect.origin.y;
       		
       		// Check position of the screen, adjust rect accordingly
       		if 	(
       			[[UIApplication sharedApplication]statusBarOrientation] == UIDeviceOrientationLandscapeLeft ||
       			[[UIApplication sharedApplication]statusBarOrientation] == UIDeviceOrientationLandscapeRight
       			)
       		{
       			beginRect.size.width = beginWidth;
       			beginRect.size.height = beginHeight;
       			beginRect.origin.x = beginY;
       			beginRect.origin.y = beginX;
       
       			endRect.size.width = endWidth;
       			endRect.size.height = endHeight;
       			endRect.origin.x = endY;
       			endRect.origin.y = endX;
       
       		}
       
       		// Create and NSDictionary with both rects
       		NSDictionary *keyboardBeginRect = [NSDictionary dictionaryWithObjectsAndKeys:
       								[NSNumber numberWithFloat:beginRect.size.width], @"width",
       								[NSNumber numberWithFloat:beginRect.size.height], @"height",
       								[NSNumber numberWithFloat:beginRect.origin.x], @"x",
       								[NSNumber numberWithFloat:beginRect.origin.y], @"y",
       								nil];
       		
       		NSDictionary *keyboardEndRect = [NSDictionary dictionaryWithObjectsAndKeys:
       								[NSNumber numberWithFloat:endRect.size.width], @"width",
       								[NSNumber numberWithFloat:endRect.size.height], @"height",
       								[NSNumber numberWithFloat:endRect.origin.x], @"x",
       								[NSNumber numberWithFloat:endRect.origin.y], @"y",
       								nil];
       								
       		NSString *name = @"";
       		if([notName isEqual: UIKeyboardWillShowNotification])
       		    	name = @"willShow";
       		else
       		if([notName isEqual: UIKeyboardDidShowNotification])
       				name = @"didShow";
       		else
       		if([notName isEqual: UIKeyboardWillHideNotification])
       		    	name = @"willHide";
       		else
       		if([notName isEqual: UIKeyboardDidHideNotification])
       				name = @"didHide";	
       		else
       		if([TiUtils isIOS5OrGreater] == YES && [notName isEqual: UIKeyboardWillChangeFrameNotification])
       				name = @"willChange";
       		else
       		if([TiUtils isIOS5OrGreater] == YES && [notName isEqual: UIKeyboardDidChangeFrameNotification])
       				name = @"didChange";
       		else
       				name = notName;
       																	
       		// Create the dictionary for the event
       		NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
       				name, @"name",
       				keyboardEndRect, @"endRect",
       				keyboardBeginRect, @"beginRect",
       				[userInfo valueForKey:UIKeyboardAnimationCurveUserInfoKey], @"animationCurve",
       				[userInfo valueForKey:UIKeyboardAnimationDurationUserInfoKey], @"animationDuration",
       				nil];
       				
       		// Fire event
       		[self fireEvent:@"keyboard" withObject:dict];
       	}
       }
       
    The Javascript would be something like this:
       Ti.App.addEventListener('keyboard', function(e){
       	var name = e.name;
       	var bRect = e.beginRect;
       	var eRect = e.endRect;
       	var aCurve = e.animationCurve;
       	var aDuration = animationDuration;
       
       	switch(name){
       		case 'willShow':
       			// some code
       		break;
       		case 'didShow':
       			// some code
       		break;
       		case 'willHide':
       		// some code
       		break;
       		case 'didHide':
       			// some code
       		break;
       
       		// iOS 5 - iPad
       		case 'willChange':
       			// some code
       		break;
       		case 'didChange':
       			// some code
       		break;
       	}
       });
       
  3. Sabil Rahim 2012-05-22

    Testing Code
       var win = Ti.UI.createWindow({
         backgroundColor: 'white',
         exitOnClose: true,
         fullscreen: false,
         layout: 'vertical',
         title: 'KeyboardFrameChanged Demo'
       });
       
       var textField = Ti.UI.createTextField({
       	color: '#336699',
       	height: 60,
       	top: 10,
       	left: 10,
       	width: 250,
       	borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED
       });
       
       win.add(textField);
       Ti.App.addEventListener('keyboardFrameChanged', function(e){
       	Ti.API.info("Keyboard Frame Size Changed\n");
       	Ti.API.info("X :"+ e.keyboardFrame.x + "\ny:"+e.keyboardFrame.y+"\nHeight:"+e.keyboardFrame.height+"\nWidth:"+e.keyboardFrame.width);
       }); 
       
       win.open();
       
  4. Blain Hamon 2012-05-24

    Pull merged
  5. Natalie Huynh 2012-06-06

    Tested with iPad 3 5.1.1 and iPhone 3gs 4.3

JSON Source