键盘显示后我的滚动怎么样不起作用?

时间:2014-03-21 19:05:48

标签: ios iphone uiscrollview uitextfield uitextfielddelegate

所以我实施了Apple推荐的way管理键盘。

但如果activeTextfield位于键盘后面,我的视图仍然无法向上或向下滚动。我觉得我错过了一些基本的东西。

这就是为什么我决定在下面发布我的所有代码。有人可以指出我做错了吗?

这是我的.m文件中的代码:

- (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);
self.scrollView.contentInset = contentInsets;
self.scrollView.scrollIndicatorInsets = contentInsets;

// If active text field is hidden by keyboard, scroll it so it's visible
// Your app might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
    [self.scrollView scrollRectToVisible:activeField.frame animated:YES];
}
}

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

我在.h文件中声明了正确的滚动视图,如下所示:

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

*请注意我在视图内部的滚动视图(此视图属性未在.h中声明)在视图控制器中。

我已实施以下内容:

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

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

在将所有文本字段嵌入滚动视图之前,下面的代码用于重新设置键盘。但是,它现在无法运作。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

UITouch *touch = [[event allTouches] anyObject];
 NSLog(@"TOUCH TOUCH");
if ([_cardnumbertext isFirstResponder] && [touch view] != _cardnumbertext) {
    [_cardnumbertext resignFirstResponder];
    NSLog(@"TOUCH TOUCH0");
}

1 个答案:

答案 0 :(得分:0)

您是否在滚动视图上设置了contentSize属性?请在调用scrollToRectVisible:animate:。

之前尝试放置此行
[self.scrollView setContentSize:CGSizeMake(self.scrollView.contentSize.width, self.scrollView.frame.size.height + kbSize.height)];