JNYJ

JNYJ - IOS - DEV

 

怎么自动移出被键盘覆盖(遮盖)的控件 NO.1(Category),适用于UITableView

@implementation UITableView(AnimationFashion)

- (void) scrollViewIfControlCoveredByKeyboard:(UIControl *)control atIndexPath:(NSIndexPath *)indexPath  hasStatusBar:(BOOL)hasStatusBar hasNavigationBar:(BOOL)hasNavBar{

	CGFloat keyboardHeight = 216.0f;
	CGFloat navBarHeight = 44.0f;
	CGFloat statusBarHeight = 20.0f;
	CGFloat screenHeight = 480.0f;
	
	ViewOriginFrame = self.frame;
	CGFloat	controlYRelativeToScreen = (((CGRect)[self rectForRowAtIndexPath:indexPath]).origin.y + control.frame.origin.y) + self.frame.origin.y + control.frame.size.height;
	
	NSLog(@"Cell Y %f",((CGRect)[self rectForRowAtIndexPath:indexPath]).origin.y);
	NSLog(@"Self Y %f",control.frame.origin.y);
	NSLog(@"Tbl  Y %f",self.frame.origin.y);
	NSLog(@"Self H %f",control.frame.size.height);
	NSLog(@"All  H %f",controlYRelativeToScreen);
	
	if (hasStatusBar) {
		controlYRelativeToScreen += statusBarHeight;
	}
	if (hasNavBar) {
		controlYRelativeToScreen += navBarHeight;
	}
	CGFloat invisiblePixels = keyboardHeight - (screenHeight - controlYRelativeToScreen) - self.bounds.origin.y;//
	if (invisiblePixels > 0) {
		//in this case, the control is covered by keyboard, so scroll view to make it visible.
		[UIView beginAnimations:@"scrollView" context:nil];
		[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
		[UIView setAnimationDuration:0.275f];
		self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y - invisiblePixels, self.frame.size.width, self.frame.size.height);
		[UIView commitAnimations];
	}
}
- (void) restoreViewPosition{
	
	[UIView beginAnimations:@"restoreViewPosition" context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDuration:0.200f];
	self.frame = ViewOriginFrame;
	[UIView commitAnimations];
}

@end

posted on 2011-03-04 17:36  JNYJ  阅读(817)  评论(0编辑  收藏  举报

导航