可编辑的webView自动滚动到顶部

时间:2012-07-17 10:01:50

标签: ipad ios5 uiwebview contenteditable

我在可编辑的webView中显示了一些文字。只要我向下滚动并触摸某处以编辑渲染文本,它就会滚动到顶部并显示键盘,因此我必须再次向下滚动以进行编辑。有没有办法阻止webView这样做?

4 个答案:

答案 0 :(得分:6)

遇到同样的问题,仍在寻找这种奇怪行为的正常解决方案。 我们仍然无法阻止UIWebView这样做,如果你看看iPad上的Evernote应用程序,你会看到同样的问题,不幸的是:( 我们唯一能做的就是在显示键盘时保存UIWebView的contentOffset,并在键盘打开后恢复。

这看起来像是:

//register your controller for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) UIKeyboardDidShowNotification object:nil];

然后你需要处理键盘通知,如:

- (void)keyboardWillShow:(NSNotification *)aNotification {

// scroll view will scroll to beginning, but we save current offset
[_yourViewWithWebView saveOffset];
    ...
}

之后,您需要在显示键盘时处理事件:

- (void)keyboardWasShown:(NSNotification*)aNotification{
...
// scroll view scrolled to beginning, but we restore previous offset
[_yourViewWithWebView restoreOffset];
}

因此,在包含UIWebView的视图中,您需要实现:

static CGPoint editableWebViewOffsetPoint;

- (void) saveOffset{
    editableWebViewOffsetPoint = yourWebView.scrollView.contentOffset;
}

- (void) restoreOffset{
    //just use animation block to have scroll animated after jumping to top and back to old position
    [UIView animateWithDuration:.2
            delay:0
            options:UIViewAnimationOptionCurveEaseIn
            animations:^{
                yourWebView.scrollView.contentOffset = editableWebViewOffsetPoint;
            }
            completion:nil];

}

希望一般来说这将帮助您至少部分解决问题。

如果有人会在每次键盘显示时帮助我们阻止UIWebView滚动到顶部,我会非常感激。

UIWebView.scrollView.scrollsToTop = NO;没有帮助。

在显示键盘之前禁用滚动并在显示键盘后启用它也不起作用。

此外,当光标不在UIWebView的可见区域时,您将面临编辑文本的问题 - 并且它不会自动滚动自身以使光标可见。我们已经解决了这个问题,但我正在为我们如何完成这项工作创建详细而可读的教程。如果你已经解决了这个问题,我很感激你看看你的解决方案:)

PS:http://www.cocoanetics.com/2011/01/uiwebview-must-die/

谢谢你, 谢尔盖N。

答案 1 :(得分:4)

运行良好的方法是在显示键盘时暂时禁用setContentOffset:上的UIScrollView。这有点像黑客,所以在某些情况下可能会导致其他问题。

@Sergey N.的响应一样,注册键盘通知,但不要存储/恢复contentOffset,请使用以下命令:

- (void)keyboardWillShow:(NSNotification *)aNotification {
    [self disableMethod:@selector(setContentOffset:) onClass:[UIScrollView class]];
}
- (void)keyboardWasShown:(NSNotification *)aNotification {
    [self enableMethod:@selector(setContentOffset:) onClass:[UIScrollView class]];
}

在课堂的其他地方(或其他课程中,只要您在上述调用中替换self),请放置以下内容:

-(void)swizzleMethod:(SEL)origSel from:(Class)origClass toMethod:(SEL)toSel from:(Class)toClass{
    Method origMethod = class_getInstanceMethod(origClass, origSel);
    Method newMethod = class_getInstanceMethod(toClass, toSel);
    method_exchangeImplementations(origMethod, newMethod);
}
-(void)disableMethod:(SEL)sel onClass:(Class)cl{
    [self swizzleMethod:sel from:cl toMethod:@selector(doNothing) from:[self class]];
}
-(void)enableMethod:(SEL)method onClass:(Class)cl{
    [self swizzleMethod:@selector(doNothing) from:[self class] toMethod:method from:cl];
}
-(void)doNothing{

}

这可以防止webview首先滚动到顶部,因此它不会显示坏动画,但是,在某些情况下它可能会导致一些问题(例如,在持有webview的视图中有更多的输入控件)。在iOS 5.0+中成功测试过。 在iOS 6.0中,滚动到顶部似乎是固定的,因此不需要解决方法。

答案 2 :(得分:0)

“当光标不在可见区域时编辑文本”问题的功能。

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

//if(self.navigationController.viewControllers objectAtIndex:([self.navigationController.viewControllers count]-1)==self.)
NSLog(@"keyboardshown");
if (keyboardshown)
    return;
keyboardshown=YES;
NSDictionary* userInfo = [aNotification userInfo];

CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

CGRect newFrame = self.textView.frame;
CGRect keyboardFrame = [self.textView convertRect:keyboardEndFrame toView:nil];

newFrame.size.height -= keyboardFrame.size.height;    
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];

[UIView setAnimationDuration:0.3];
[self.textView setFrame:newFrame];
[UIView commitAnimations];



}

- (void)keyboardWasHidden:(NSNotification *)aNotification {

if (!keyboardshown)
    return;
keyboardshown=NO;

NSDictionary* userInfo = [aNotification userInfo];

CGRect keyboardEndFrame;
[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];

CGRect newFrame = self.textView.frame;
CGRect keyboardFrame = [self.textView convertRect:keyboardEndFrame toView:nil];

newFrame.size.height += keyboardFrame.size.height;
[UIView beginAnimations:@"ResizeForKeyboard" context:nil];
[UIView setAnimationDuration:0.3];
self.textView.frame = newFrame;
[UIView commitAnimations];




}

答案 3 :(得分:0)

实际上并不总是会调用keyboardWasShown,特别是当用户连接了BT键盘并且可以通过弹出键隐藏/显示虚拟键盘时。我们已经实现了自己的类,如:

    @implementation KeyboardUtils

+ (CGRect) convertRect:(CGRect)rect toView:(UIView *)view {
    UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *) view : [view window];
    return [view convertRect:[window convertRect:rect fromWindow:nil] fromView:nil];
}

/**
 * This is working but deprecated solution
 * Based on UIKeyboardCenterBeginUserInfoKey and UIKeyboardCenterEndUserInfoKey which are deprecated since iOS 3.2
 */
+ (BOOL)checkKeyboardOnDisplayCenterBegin:(CGRect)centerBegin centerEnd:(CGRect)centerEnd{

    CGRect mainScreen = [UIApplication currentBounds];

    BOOL isKeyboardOnDisplay = CGRectContainsPoint(mainScreen, centerEnd.origin);

    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isKeyboardOnDisplay] forKey:@"isKeyboardOnDisplay"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    return isKeyboardOnDisplay;
}

/**
 * This method allows to verify if software keyboard is currently present on screen for the application
 * Allows to handle undocked, split states of keyboard, as well as connected Bluetooth keyboard.
 * Needed to adjust UI - scrolling and insets for editable parts of the app, as well as avoid application be beneath open keyboard
 */
+ (BOOL)checkKeyboardOnDisplayBeginFrame:(CGRect)frameBegin endFrame:(CGRect)frameEnd{
    CGRect mainScreen = [UIApplication currentBounds];
    UIView *firstView = [[(AppDelegate *)[[UIApplication sharedApplication] delegate] window].subviews objectAtIndex:0];

    CGRect convertedEndFrame = [KeyboardUtils convertRect:frameEnd toView:firstView];

    BOOL isKeyboardOnDisplay = CGRectContainsRect(mainScreen, convertedEndFrame);

    [[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:isKeyboardOnDisplay] forKey:@"isKeyboardOnDisplay"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    return isKeyboardOnDisplay;
}



+ (BOOL)checkKeyboardOnDisplayFromNotification:(NSNotification *)aNotification{
    BOOL isKeyboardOnDisplay = [KeyboardUtils checkKeyboardOnDisplayBeginFrame:[[aNotification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue] 
                                                                      endFrame:[[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]];    
    return isKeyboardOnDisplay;
}

然后你就可以使用它:

- (void)keyboardWillChangeFrame:(NSNotification*)aNotification{
    [KeyboardUtils checkKeyboardOnDisplayFromNotification:aNotification];
}

其中keyboardWillChangeFrame是选择器观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

通过这种方式,您可以将键盘的状态(如果它显示为停靠状态并且显示在屏幕上,并且不使用BT键盘)保存到NSUserDefaultSettings中。在收听键盘通知或方向更改的处理程序中,您应该从默认值中检查此键值。

另一种方法是[UIApplication currentBounds]; 它通过扩展类似于:(。h file)

在应用程序中出现
#import <UIKit/UIKit.h>

@interface UIApplication (AppDimensions)
+(CGSize) currentSize;
+(CGRect) currentBounds;
+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation;
@end

.m文件:

#import "UIApplication+AppDimensions.h"

@implementation UIApplication (AppDimensions)

+(CGSize) currentSize
{
    return [UIApplication sizeInOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

+(CGRect) currentBounds{
    CGRect bounds = [UIScreen mainScreen].bounds;
    bounds.size = [UIApplication currentSize];
    return bounds;
}

+(CGSize) sizeInOrientation:(UIInterfaceOrientation)orientation
{
    CGSize size = [UIScreen mainScreen].bounds.size;
    UIApplication *application = [UIApplication sharedApplication];
    if (UIInterfaceOrientationIsLandscape(orientation))
    {
        size = CGSizeMake(size.height, size.width);
    }
    if (application.statusBarHidden == NO)
    {
        size.height -= MIN(application.statusBarFrame.size.width, application.statusBarFrame.size.height);
    }
    return size;
}

@end

希望这可以帮助任何关心在屏幕上处理键盘存在的人。