【问题标题】:iOS webView inside scrollView : change height depending on the device orientation滚动视图内的iOS webView:根据设备方向更改高度
【发布时间】:2017-07-18 10:00:51
【问题描述】:

我在scrollView 中有一个webView(scrollView > mainView > webView 和一个包含其他组件的 view2)。为了只有一个滚动条(scrollView 之一),我在完成加载时计算 webView 的高度,然后设置 mainView 的高度(它的高度等于 webView + view2 的高度)。我的应用程序支持纵向和横向模式。要检查旋转何时发生变化,我使用:

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

在这个函数中,我调用另一个函数来重置我的webView 的内容(设置html 内容),以便

(void)webViewDidFinishLoad:(UIWebView *)webView.

可以召回:

- (void)webViewDidFinishLoad:(UIWebView *)webView {

    //set the main view height
    [self.loader stopAnimating];
    CGSize contentSize = webView.scrollView.contentSize;
    float scrollHeight = contentSize.height + 270;
    NSLog(@"webViewDidFinishLoad scrollHeight %f", scrollHeight);
    self.mainViewHeight.constant = scrollHeight;

}

我的问题:当应用程序一开始处于portrait 模式时,scrollView 的高度是正确的并且可以正常工作。之后,当我将旋转更改为landscape 模式时,高度也是正确的,但是当我将设备返回到portrait 时,scrollView 保持landscape 模式的高度,即使我设置了webViewDidFinishLoad 中的高度。当我在self.mainViewHeight.constant = scrollHeight; 中设置断点时,我发现当我从横向模式转到纵向模式时,contentSize 的高度并没有改变

【问题讨论】:

  • 检查您的支持设备方向,也许您不支持例如颠倒,但旋转到它并等待一些东西
  • @gaRik 但我在其他屏幕上没有这个问题(在同一设备上的同一应用程序中)

标签: ios objective-c webview uiscrollview


【解决方案1】:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
   // change the frame for both portrait and landscape
   // check the orientation
   if(toInterfaceOrientation == UIDeviceOrientationPortrait){
    webView.frame = self.view.frame;
   }else {
    //
   }
}

【讨论】:

  • 将此添加到 html 内容的头部
  • 即使我的问题是高度而不是宽度?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-16
  • 2018-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-11
  • 2011-06-19
相关资源
最近更新 更多