Most of the time, we are worried about shielding the keyboard from screens that are not big enough. Especially when the user is in the editing status, the content under the keyboard cannot be seen, and the user can only be blind. Now there is a simple solution. The basic idea is to add notifications. Listen to Keyboard Events all the time. When the keyboard is blocked, move the editor up to the height of the keyboard. When the keyboard disappears, the editing area replies to the original location. OK. Let's get two pieces of code.
1 - (void)viewDidLoad 2 { 3 [super viewDidLoad]; 4 // Do any additional setup after loading the view, typically from a nib. 5 self.textView=[[UITextView alloc]initWithFrame:self.view.frame]; 6 self.textView.text=@"请输入文字"; 7 [self.view addSubview:self.textView]; 8 } 9 10 - (void)didReceiveMemoryWarning 11 { 12 [super didReceiveMemoryWarning]; 13 // Dispose of any resources that can be recreated. 14 } 15 16 - (void)viewWillAppear:(BOOL)animated 17 { 18 //注册通知,监听键盘出现 19 [[NSNotificationCenter defaultCenter]addObserver:self 20 selector:@selector(handleKeyboardDidShow:) 21 name:UIKeyboardDidShowNotification 22 object:nil]; 23 //注册通知,监听键盘消失事件 24 [[NSNotificationCenter defaultCenter]addObserver:self 25 selector:@selector(handleKeyboardDidHidden) 26 name:UIKeyboardDidHideNotification 27 object:nil]; 28 [super viewWillAppear:YES]; 29 } 30 31 //监听事件 32 - (void)handleKeyboardDidShow:(NSNotification*)paramNotification 33 { 34 //获取键盘高度 35 NSValue *keyboardRectAsObject=[[paramNotification userInfo]objectForKey:UIKeyboardFrameEndUserInfoKey]; 36 37 CGRect keyboardRect; 38 [keyboardRectAsObject getValue:&keyboardRect]; 39 40 self.textView.contentInset=UIEdgeInsetsMake(0, 0,keyboardRect.size.height, 0); 41 } 42 43 - (void)handleKeyboardDidHidden 44 { 45 self.textView.contentInset=UIEdgeInsetsZero; 46 } 47 48 - (void)viewDidDisappear:(BOOL)animated 49 { 50 [[NSNotificationCenter defaultCenter] removeObserver:self]; 51 }
Textview move up as the keyboard pops up