这可能不是一个容易回答的问题......
我在屏幕上有 8 个文本字段,它们位于滚动视图中。当我选择第一个字段时,键盘出现并且滚动视图缩小。然后我可以上下滚动。
当我选择另一个文本字段时,我不能再滚动了。
我已经检查了滚动视图的高度textFieldDidBeginEditing
,它肯定会重新长出来。
我还注册了所有 4 个键盘通知,但在移动到新文本字段期间都没有触发它们。
任何指针将不胜感激:)
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:self.view.window];
keyboardIsShown = NO;
//Additional Code
}
- (void)viewDidUnload {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardWillHide:(NSNotification *)n
{
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height += (keyboardSize.height - self.toolBar.frame.size.height);
[self.scrollView setFrame:viewFrame];
keyboardIsShown = NO;
}
- (void)keyboardWillShow:(NSNotification *)n
{
if (keyboardIsShown) {
return;
}
NSDictionary* userInfo = [n userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect viewFrame = self.scrollView.frame;
viewFrame.size.height -= (keyboardSize.height - self.toolBar.frame.size.height);
scrollViewHeight = viewFrame.size.height;
[self.scrollView setFrame:viewFrame];
keyboardIsShown = YES;
}
在文本字段之间移动:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[[self saveButton] setEnabled:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.label1) {
[self.label2 becomeFirstResponder];
} else if (textField == self.label2) {
[self.label3 becomeFirstResponder];
} else if (textField == self.label3) {
[self.label4 becomeFirstResponder];
} else if (textField == self.label4) {
[self.label5 becomeFirstResponder];
} else if (textField == self.label5) {
[self.label6 becomeFirstResponder];
} else if (textField == self.label6) {
[self.label7 becomeFirstResponder];
} else if (textField == self.label7) {
[self.label8 becomeFirstResponder];
} else if (textField == self.label8) {
[textField resignFirstResponder];
}
return YES;
}