JNYJ

JNYJ - IOS - DEV

 

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

@implementation UIViewController(AnimationFashion)
#pragma mark 
#pragma mark implementation
//regard the control as child view of self by default.
- (void) scrollViewIfControlCoveredByKeyboard:(UIControl *)control hasStatusBar:(BOOL)hasStatusBar hasNavigationBar:(BOOL)hasNavBar{
	CGFloat keyboardHeight = 216.0f;
	CGFloat navBarHeight = 44.0f;
	CGFloat statusBarHeight = 20.0f;
	CGFloat screenHeight = 480.0f;
	
	ViewOriginFrame = self.view.frame;
	CGFloat	controlYRelativeToScreen = control.frame.origin.y + self.view.frame.origin.y + control.frame.size.height;
	if (hasStatusBar) {
		controlYRelativeToScreen += statusBarHeight;
	}
	if (hasNavBar) {
		controlYRelativeToScreen += navBarHeight;
	}
	CGFloat invisiblePixels = keyboardHeight - (screenHeight - controlYRelativeToScreen);
	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.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y - invisiblePixels, self.view.frame.size.width, self.view.frame.size.height);
		[UIView commitAnimations];
	}
}

- (void) restoreViewPosition{
	[UIView beginAnimations:@"restoreViewPosition" context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationDuration:0.200f];
	self.view.frame = ViewOriginFrame;
	[UIView commitAnimations];
}
- (void) didPopUpView:(id)sender{
	if(self.navigationController != nil){
		[self.navigationController popViewControllerAnimated:YES];
	}
}
@end

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

导航