当键盘出现时,UIScrollbar不会向上滚动
问题描述:
我有一个故事板,里面插入了一个滚动条,因为要显示的内容太多而不能一次出现。 滚动条包含:当键盘出现时,UIScrollbar不会向上滚动
- 的图像视图
- 标签
- 分组的表视图
表视图中的行两种。第一种(由控制器regularRegistrationCellVC定义)包含一个文本字段和一个标签。 当我点击任何生成的单元格中的任何文本字段时,键盘出现并且textfield变为隐藏。此外,不可能向上滚动视图的内容以查看键盘下的内容。 所以,以下苹果的指示,添加此代码到包含滚动条,所述控制器:
注册为被叫键盘通知
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
当UIKeyboardDidShowNotification发送
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollPanel.contentInset = contentInsets;
scrollPanel.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollPanel setContentOffset:scrollPoint animated:YES];
}
}
键盘消失时调用
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollPanel.contentInset = contentInsets;
scrollPanel.scrollIndicatorInsets = contentInsets;
}
管理编辑开始的事件
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
NSLog(@"inizio digitazione\n");
}
结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
其中activeField是我的控制器的一个全局变量的事件管理。
然后,我修改的代码中创建的第一类单元的一部分,以这样一种方式,第一种的每一个新的细胞有我的视图控制器为委托:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
static NSString *CellIdentifier = @"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [self.orientationItems objectAtIndex:indexPath.row];
cell.regularTextField.delegate = self;
return cell;
}
else{
static NSString *CellIdentifier = @"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [self.orientationItems objectAtIndex:[orientationItems count]-1];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:@"orientation"];
if (val == nil)
cell.orientationLabel.text = @"Eterosessuale";
else
cell.orientationLabel.text = val;
return cell;
}
}
最后,我宣布,我视图控制器(包括滚动条及其内容)实现 UITextFieldDelegate。 使用NSLog打印,我发现每次单击文本字段时,管理事件的函数肯定会执行。
你能告诉我错在哪?
感谢您的帮助