gpt4 book ai didi

iphone - 当键盘覆盖事件的 UITextField 时 UIScrollView 不滚动(使用苹果的例子)

转载 作者:搜寻专家 更新时间:2023-10-30 20:26:26 25 4
gpt4 key购买 nike

我是 iOS 编程的新手,在编辑被键盘遮挡的 UITextField 时,我无法移动 UIScrollView。代码直接出Apple's documentation但由于某种原因它不起作用。

通过调试我发现通知似乎被正确传递(即它记录“ View 应该调整大小”,但仅当 activeField 是键盘下方的文本字段时)并且滚动点设置正确,但是scrollview 还是不动。另外,我有理由相信委托(delegate)模式是正确的(ViewController 是 textFields 和 scrollView 的委托(delegate))

- (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);
scrollView.contentInset = contentInsets;
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;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
NSLog(@"%@",@"view should resize");
}
}

鉴于代码直接来自文档,我可能只是遗漏了一些简单的东西。谁能指出我要检查的方向?

最佳答案

Apple 的示例有一个错误,它没有明确设置 ScrollView 的内容大小,因此使用默认内容大小 (0, 0)。我通过在我的 View Controller 中添加这段代码解决了这个问题:

- (void)viewDidLoad
{
[super viewDidLoad];

// Set the scroll view's content size to be the same width as the
// application's frame but set its height to be the height of the
// application frame minus the height of the navigation bar's frame
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGRect navigationFrame = [[self.navigationController navigationBar] frame];
CGFloat height = applicationFrame.size.height - navigationFrame.size.height;
CGSize newContentSize = CGSizeMake(applicationFrame.size.width, height);

((UIScrollView *)self.view).contentSize = newContentSize;
}

关于iphone - 当键盘覆盖事件的 UITextField 时 UIScrollView 不滚动(使用苹果的例子),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8528134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com