0

更新 - 解决方案 2

第一个解决方案(如下)仅适用于 iOS 7,不适用于 iOS 6 或 iOS 5。因此,另一种方法是在发生任何滚动移动时使用 更新滚动视图的内容大小-(void)scrollViewDidScroll:(UIScrollView *)scrollView,适用于 iOS 7、6 和 5。

在里面-(void)scrollViewDidScroll:(UIScrollView *)scrollView,我记录了最高的内容大小高度,

if (_currentWebPageContentHeight < [[_webView scrollView] contentSize].height) {
        _currentWebPageContentHeight = [[_webView scrollView] contentSize].height;
    }

并在加载另一个网页或重新加载当前页面时恢复_currentWebPageContentHeight到默认高度(网页视图的高度)。

更新 - 解决方案 1

我找到了一种解决方法,但不知道它为什么有效。稍微改变contentInset滚动视图会强制刷新contentSize到当前视图。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    /// Load image data in async manner from photo chosen in album or taken by camera
    // ....

    /// Refresh the contentSize of UIScrollView inside UIWebView
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [_galleryPopoverController dismissPopoverAnimated:YES];

        /// A patch to force refresh the contentSize of scrollView, 0.01 is chosen to give unnoticeable layout change to user
        [[webView scrollView] setContentInset:UIEdgeInsetsMake(0, 0, 0.01, 0)]; // UIEdgeInsetsMake(top, left, bottom, right)
    } else {
        [picker dismissViewControllerAnimated:YES completion:^{
            /// A patch to force refresh the contentSize of scrollView, 0.01 is chosen to give unnoticeable layout change to user
            [[webView scrollView] setContentInset:UIEdgeInsetsMake(0, 0, 0.01, 0)]; // UIEdgeInsetsMake(top, left, bottom, right)
          }];
    }
}

最初的问题

使用 Xcode 5、iOS 7、iPhone 5,没有 nib 文件,没有手动添加自动布局约束。

在一个项目中,有一个 UIWebView,左下角有一个按钮,该按钮用于将照片从 iPhone 的相册或相机上传到测试网站。

Choose当我尝试从相册上传照片时单击按钮时,-(void)imagePickerController:didFinishPickingMediaWithInfo:会调用该方法,当该方法完成时,UIImagePickerController 会随着动画消失,并加载选择的照片。但是 UIWebView 的 UIScrollView 子视图,虽然可以向下滚动到当前网页的末尾,但是一直弹回到 UIWebView 的可见帧(origin.x = 0, origin.y = 64, size.width = 320, size.height = 568 - 64),也看不到垂直滚动指示器。

嵌入在 UIWebView 中的 UIScrollView 的框架/边界是正确的,我在以下语句的 ^{} 中检查它:

// At the end of the -(void)imagePickerController:didFinishPickingMediaWithInfo:
[imagePickerControllerInstance dismissViewControllerAnimated:YES completion:^{}];

我还强制这些值是正确的。但不会改变上述意外行为。

[[webView scrollView] setShowsVerticalScrollIndicator:YES];
[[webView scrollView] setScrollIndicatorInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[webView scrollView] setContentSize:(CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height - 64))];

关于上面使用的数字:

  • 320 是 iPhone 5 的宽度(以磅为单位)。
  • 568 是 iPhone 5 的高度(以磅为单位)。
  • 64 是状态栏和导航栏的高度(以磅为单位)。

任何人都可以解决这个问题?谢谢!

4

1 回答 1

2

您应该setContentSize:使用实际的内容大小,而不是可见的帧大小。也就是说,如果你的内容是 1000px 高,你应该setContentSize:CGSizeMake(width, 1000). 通过这种方式,您告诉 aUIScrollView它应该滚动以显示 1000 像素高的内容。如果您将 aUIScrollView的内容大小设置为等于其框架,则不会执行滚动并且不会出现滚动指示符(仅弹跳效果)。

于 2013-11-07T09:41:28.390 回答