0

当我点击文本字段并出现键盘时,我正在尝试编写一些代码以使视图向上移动。包含文本字段的视图不是程序启动时出现的第一个视图。所以我在 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

因此,当我点击这些方法的文本字段时,程序会执行这些方法但没有任何反应,我的意思是视图没有被键盘移动,我不能让键盘消失。为什么这样?

4

3 回答 3

0

尝试使用bounds而不是frame. 锚定可能会妨碍您。

于 2012-04-22T12:59:00.243 回答
0

你想让它向上移动还是改变大小?这行得通吗?

-(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-keyboardSize.height, gm.frame.size.width, gm.frame.size.height);
}

-(void)keyboarDidHide:(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+keyboardSize.height, gm.frame.size.width, gm.frame.size.height);
}
于 2012-04-22T13:11:12.623 回答
0

这段代码对我有用:

- (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;

    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, webView.frame.size.height-keyboardSize.height);
}

-(void)keyboardDidHide:(NSNotification*)notification {
    NSDictionary* userInfo = [notification userInfo];
    NSValue* value = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;

    webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y, webView.frame.size.width, webView.frame.size.height+keyboardSize.height);
}
于 2012-04-22T14:58:31.323 回答