在ios7中为UIScrollView忽略contentInset

时间:2014-01-14 00:46:04

标签: ios objective-c uiscrollview

这在ios7之前有效,当有人在UIScrollView中点击任何可能成为第一响应者的东西时。现在它没有 - UITextFields / Views仍然可以在键盘下显示。

代码:

- (void)keyboardWasShown:(NSNotification*)notification{

//Some similar questions mentioned this might work, but made no difference for me
self.automaticallyAdjustsScrollViewInsets=NO;

NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

float height = 0.0;

if (UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
    height = kbSize.width;
} else {
    height = kbSize.height;
}

UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, height, 0.0);


[UIView animateWithDuration:.25
                      delay:0
                    options:(UIViewAnimationOptionAllowUserInteraction)
                 animations:^
 {
     self.editorScrollView.contentInset = contentInsets;
     self.editorScrollView.scrollIndicatorInsets = contentInsets;
 }
                 completion:^(BOOL finished)
 {

 }];
}

目前,使用此代码时,uitextfield / view被指定为第一响应者状态时不会发生任何事情。插图似乎没有改变 - 我也许可以使用contentOffset,但我必须找到原始视图的Y,它才能成为第一个响应者。

就像我说的那样,在ios7之前这段代码工作了(当分配了第一响应者状态时,没有文本字段/视图隐藏在键盘后面)。我似乎错过了一些明显的东西,或者在ios7中有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

检测键盘更改和帧的更好方法。

关键点是转换键盘框架:CGRect keyboardFrameInsideView = [self.view convertRect:keyboardFrame fromView:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardFrameWillChange:)
                                             name:UIKeyboardWillShowNotification
                                           object:nil];


- (void)keyboardFrameWillChange:(NSNotification *)notification
{
  CGRect keyboardFrame;
  [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];

  CGRect keyboardFrameInsideView = [self.view convertRect:keyboardFrame fromView:nil];
  CGRect r = self.bodyView.frame;
  r.size.height = CGRectGetMinY(keyboardFrameInsideView) - r.origin.y;
  self.bodyView.frame = r;
}
相关问题