objective-c - 如何在 objective-c 中出现键盘时 ScrollView ?

标签 objective-c ios keyboard scroll

我试过了,但是 View 太小,无法向上滚动。我怎样才能滚动更多?

// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillBeHidden:)
                                                 name:UIKeyboardWillHideNotification object:nil];

}

// Called when the UIKeyboardDidShowNotification is sent.
- (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);
scroll.contentInset = contentInsets;
scroll.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, EPostaText.frame.origin) ) {
    CGPoint scrollPoint = CGPointMake(0.0, EPostaText.frame.origin.y-(aRect.size.height));
    [scroll setContentOffset:scrollPoint animated:YES];
}
}

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scroll.contentInset = contentInsets;
scroll.scrollIndicatorInsets = contentInsets;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
EPostaText = textField;

}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
EPostaText = nil;
}

最佳答案

最好的方法 - 将 ScrollView 容器调整到可见区域并使用它:

- (void)scrollRectToVisible:(CGRect)rect animated:(BOOL)animated

关于objective-c - 如何在 objective-c 中出现键盘时 ScrollView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9002781/

相关文章:

ios - iPad:如何以编程方式获取以太网 IP 地址

iphone - 如何更改 UISearchBar 以用文本替换文本字段内的搜索图标

ios - NSPredicate 上的正则表达式匹配失败

ios - 从 UINavigationItem 后退按钮中删除 "<"

ios - SpriteKit : Hashing a Recycled SKShapeNode

ios - 在 iPhone 5s iOS 8.1.2 和模拟器 8.1 上找不到支持类型 4 和 5 的键盘

objective-c - 将 CoreData 添加到现有项目

ios - 线程1 : Exc_BAD_ACCESS(code=2, 地址=xxx-xxxx)

android - 使用 webview 时在 Android 中禁用 'go' 按钮表单提交

ios键盘高度不同