当我点击文本字段并出现键盘时,我正在尝试编写一些代码以使视图向上移动。包含文本字段的视图不是程序启动时出现的第一个视图。所以我在 ViewController 中写了一些代码
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
-(void)keyboardDidShow:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
NSValue* value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
gm.frame = CGRectMake(gm.frame.origin.x, gm.frame.origin.y, gm.frame.size.width, gm.frame.size.height-keyboardSize.height);
}
-(void)keyboarDidHide:(NSNotification*)notification {
gm.frame = CGRectMake(gm.frame.origin.x, gm.frame.origin.y, gm.frame.size.width, gm.frame.size.height);
}
gm 是包含文本字段的视图:
@class gameOverMenu;
@interface RootViewController : UIViewController {
gameOverMenu* gm;
}
@end
因此,当我点击这些方法的文本字段时,程序会执行这些方法但没有任何反应,我的意思是视图没有被键盘移动,我不能让键盘消失。为什么这样?