問題1:
由于UITextView作為UIScrollView的子控件纺荧,使用常規(guī)的收起鍵盤的方法在這里就會行不通。我嘗試了重寫touchesBegan方法颅筋,點擊空白沒響應宙暇,因為我實際上點的是scrollView。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
解決方案:
在Scroll View 和 Text View之間添加一層View, 將View的class設置為UIControl, 為其Touch Down事件添加相應方法:
- (IBAction)viewTouchDown:(id)sender {
[self.view endEditing:YES];
}
問題2:
顯示鍵盤的時候垃沦,鍵盤會遮擋UITextView, 我想要的效果是客给,顯示鍵盤的時候,可以通過scrollView的滾動條滾動顯示出整個UITextView肢簿。
解決方案:
在顯示和隱藏鍵盤的時候添加監(jiān)聽事件靶剑,在監(jiān)聽方法中調整scrollView的Frame。
- (void)viewWillAppear:(BOOL)animated{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardDidHidden:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyBoardDidShow:(NSNotification *)notification {
//獲取鍵盤高度
NSValue *value = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect scrollViewFrame = self.scrollView.frame;
scrollViewFrame.size.height = self.scrollView.bounds.size.height - keyboardSize.height;
self.scrollView.frame = scrollViewFrame;
}
- (void)keyBoardDidHidden:(NSNotification *)notification {
//獲取鍵盤高度
NSValue *value = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGRect scrollViewFrame = self.scrollView.frame;
scrollViewFrame.size.height = self.scrollView.bounds.size.height + keyboardSize.height;
self.scrollView.frame = scrollViewFrame;
}