• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 移动到另一个文本字段时,为什么 UIScrollView 会重新调整大小 - iOS iPhone

[复制链接]
菜鸟教程小白 发表于 2022-12-12 10:47:55 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这可能不是一个容易回答的问题......

我在屏幕上有 8 个文本字段,它们位于 ScrollView 中。当我选择第一个字段时,键盘出现并且 ScrollView 缩小。然后我可以上下滚动。

当我选择另一个文本字段时,我无法再滚动。

我已经使用 textFieldDidBeginEditing 检查了 scrollView 的高度,它肯定会长回来。

我还注册了所有 4 个键盘通知,但在移动到新文本字段期间没有一个被触发。

任何指针将不胜感激

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(keyboardWillShow
                                             name:UIKeyboardWillShowNotification
                                           object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(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)keyboardWillHideNSNotification *)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)keyboardWillShowNSNotification *)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)textFieldDidBeginEditingUITextField *)textField
{
[[self saveButton] setEnabled:YES];

}

-(BOOL)textFieldShouldReturnUITextField *)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;
}



Best Answer-推荐答案


经过大量挖掘,我找到了以下代码,我现在已经对其进行了调整和测试:

- (void)viewDidLoad
{
[super viewDidLoad];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(keyboardWasShown
                                             name:UIKeyboardDidShowNotification
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selectorselector(keyboardWillBeHidden
                                             name:UIKeyboardWillHideNotification
                                           object:nil];


keyboardIsShown = NO;


//Additional code
}

- (void)viewDidUnload {

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardDidShowNotification
                                              object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:UIKeyboardWillHideNotification
                                              object:nil];
}

- (void)keyboardWasShownNSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, (kbSize.height -    self.toolBar.frame.size.height), 0.0);
self.scrollView.contentInset = contentInsets;
self.scrollView.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 + (self.toolBar.frame.size.height*2));
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
    [self.scrollView setContentOffset:scrollPoint animated:YES];
}
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHiddenNSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;
}

-(void)textFieldDidBeginEditingUITextField *)textField
{
activeField = textField;
}

- (void)textFieldDidEndEditingUITextField *)textField
{
activeField = nil;
}

关于ios - 移动到另一个文本字段时,为什么 UIScrollView 会重新调整大小 - iOS iPhone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16354178/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap