键盘不会移动视图

时间:2012-04-22 12:56:30

标签: objective-c ios textfield

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

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

3 个答案:

答案 0 :(得分:0)

尝试使用bounds代替frame。锚定可能会阻碍。

答案 1 :(得分: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);
}

答案 2 :(得分: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);
}