TextView随键盘弹出上移高度
很多时候我们都在为键盘遮挡了原本就不大的屏幕时而烦恼,特别是当用户处于编辑状态时,键盘下面的内容就看不见了,用户只能处于盲打状态了。现在有一种简单的解决办法,基本思路就是,添加通知。一直监听键盘事件,在键盘遮挡时,将编辑器上移键盘的高度,键盘消失时,编辑区回复原来位置,ok,来两段代码吧
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 }
如果你错过了一天,那么你就真的错过了一天……ues.hk