Ios UIScrollbar没有';当键盘出现时,不要向上滚动
我有一个故事板,里面插入了一个滚动条,因为要显示的内容太多,无法一次全部显示出来。 滚动条包含:Ios UIScrollbar没有';当键盘出现时,不要向上滚动,ios,keyboard,scrollbar,Ios,Keyboard,Scrollbar,我有一个故事板,里面插入了一个滚动条,因为要显示的内容太多,无法一次全部显示出来。 滚动条包含: 图像视图 标签 分组表视图 表视图的行有两种。第一类(由控制器regularRegistrationCellVC定义)包含一个文本字段和一个标签。 当我在任何生成的单元格中单击任何文本字段时,键盘出现,文本字段隐藏。此外,无法向上滚动视图内容以查看键盘下的内容。 因此,按照苹果的指示,我在包含滚动条的控制器中添加了以下代码: 注册键盘通知 - (void)registerForKeyboardN
- 图像视图
- 标签
- 分组表视图
- (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;
}
结束编辑事件的管理
- (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打印,我发现每次单击文本字段时,管理事件的函数肯定会执行
你能告诉我哪里错了吗
谢谢你的帮助你试过了吗
只是为了修正这个答案中的几行