Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 键盘就位时移动UIScrollView_Iphone_Objective C_Ios_Ipad_Keyboard - Fatal编程技术网

Iphone 键盘就位时移动UIScrollView

Iphone 键盘就位时移动UIScrollView,iphone,objective-c,ios,ipad,keyboard,Iphone,Objective C,Ios,Ipad,Keyboard,[编辑:]问题已经解决。我没有在UIBuilder中正确链接我的代理。代码很好 当键盘出现时,我试图调整滚动视图的大小。我去了开发者文档,找到了这些信息 左边是“管理键盘” 在文档中,它显示了一些代码,用于检测键盘的大小,然后调整UIScrollView的大小。我在函数的代码中放置了一条NSLog消息-(void)键盘显示:(NSNotification*)通知,因此我看到函数实际上正在被调用,但当我尝试调用NSLog时,kbSize。height的值始终为0 为什么苹果为此提供的代码不起作用

[编辑:]问题已经解决。我没有在
UIBuilder
中正确链接我的代理。代码很好

当键盘出现时,我试图调整滚动视图的大小。我去了开发者文档,找到了这些信息

左边是“管理键盘”

在文档中,它显示了一些代码,用于检测键盘的大小,然后调整
UIScrollView
的大小。我在函数的代码中放置了一条
NSLog
消息
-(void)键盘显示:(NSNotification*)通知
,因此我看到函数实际上正在被调用,但当我尝试调用
NSLog
时,
kbSize
。height的值始终为0

为什么苹果为此提供的代码不起作用

- (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];
    }
}

您是否为该特定通知添加了观察员?确保在
loadView
方法中执行以下操作:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
不要忘记在
viewDidUnload
方法上注销观察者,如下所示:

[[NSNotificationCenter defaultCenter] removeObserver:self];

让我知道这是否可行

您可能想尝试强烈推荐的“TPKeyboardAvoidingScrollView”,可从以下网站获得:


工作起来很有魅力…

我已经用
UITableView
做过几次了(它只是一个扩展的
UIScrollView
)。你可以找到那个

一个简单的解决方案是将扩展名UIViewController+Keyboard.swift添加到项目中,只需一行

setupKeyboardNotifcationListenerForScrollView(scrollView)

当键盘出现时,它将自动调整大小。不需要子类化任何东西,只是一个扩展!它在GitHub上可用

我添加了观察者,正如我提到的,我可以看到当键盘显示时函数被调用,所以我知道观察者正在工作。谢谢你,虽然仔细检查总是很好,但是你需要更新这篇文章。viewDidUnload不再被调用。+1非常容易实现,非常好。我建议将它与BSKeyboardControl结合起来,在文本字段中添加导航控件:@Reuven使用TPKeyboardAvoidingScrollview时,有时如果我们在textview中复制和粘贴,视图会完全上升,。。有什么解决方案吗?。它是否适用于
uiPresentationModelFormSheet
呢?非常感谢,这段代码包含了我需要的所有内容:)您可以使用库来观察键盘事件。