I am trying to write some code to make the view move up when I tap on the textfield and the keyboard appears. The view, that contains the textfield, is not the first one to appear when the program starts.
So I’ve written some code in the 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 is the view that contains the textfield:
@class gameOverMenu;
@interface RootViewController : UIViewController {
gameOverMenu* gm;
}
@end
So when I tap on the textfield these methods the program goes through these methods but nothing happens, I mean the view isn’t move by keyboard and I cannot make the keyboard disappear. Why so?
Do you want it to move up or change the size? Does this work?