我无法解释代码中的错误。当我单击“提交”按钮时,键盘再次上升。这里我使用NSNotificatioCenter
根据键盘高度滚动视图。
查看下面的代码。
-(void)viewDidLoad
{
[super viewDidLoad];
[[self navigationController] setNavigationBarHidden:YES];
// Register for the events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
// Setup content size
_scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,SCROLLVIEW_CONTENT_HEIGHT);
keyboardVisible = NO;
}
-(void) viewWillDisappear:(BOOL)animated
{
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void) keyboardDidShow: (NSNotification *)notif
{
NSLog(@"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = _scrollView.contentOffset;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = _scrollView.frame;
viewFrame.size.height -= keyboardSize.height;
_scrollView.frame = viewFrame;
CGRect textFieldRect = [activeView frame];
textFieldRect.origin.y += 10;
[_scrollView scrollRectToVisible:textFieldRect animated:YES];
NSLog(@"ao fim");
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
_scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);
// Reset the scrollview to previous location
_scrollView.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}
-(BOOL) textViewShouldBeginEditing:(UITextView*)textView {
activeView = textView;
return YES;
}
-(void)textViewDidEndEditing:(UITextView *)textView{
if(textView == _textViewFeedback)
[_textViewFeedback resignFirstResponder];
else
[_textViewEmail resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
if([text isEqualToString:@"\n"]){
[textView resignFirstResponder];
}
return YES;
}
- (IBAction)BtnActionSubmitFeedback:(id)sender {
_textViewEmail.text = nil;
_textViewFeedback.text = nil;
NSString *errorMessage = [self validateForm];
if (errorMessage) {
showAlert(@"Warning", errorMessage, nil, nil, @"Dismiss");
return;
}
}
答案 0 :(得分:1)
添加UITextField的代表
@interface MyViewController : UIViewController <UITextFieldDelegate>
现在将textField.delegate = self;
设置为viewDidLoad
添加此委托方法以关闭键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
OR
您可以在提交按钮操作中添加[self.view endEditing:YES];
以关闭键盘。