【问题标题】:How to update height of my frame?如何更新我的框架的高度?
【发布时间】:2016-04-06 07:57:44
【问题描述】:

我有 UIScrollViewUIWebView

    self.scrollView = UIScrollView(frame: CGRect(x: 0, y: 64, width: CGRectGetWidth(self.view.frame), height: CGRectGetHeight(self.view.frame) - 108))
    self.scrollView.backgroundColor = .grayColor()
    self.view.addSubview(self.scrollView)

    self.webView = UIWebView(frame: CGRect(x: 0, y: 0, width: CGRectGetWidth(self.scrollView.frame), height: CGRectGetHeight(self.scrollView.frame)))
    self.webView.backgroundColor = .orangeColor()
    self.scrollView.addSubview(self.webView)

现在,我想改变我的scrollViewwebView 的高度。为此,我这样做:

func webViewDidFinishLoad(webVieww: UIWebView) {
    self.scrollView.frame.size.height = self.webView.scrollView.contentSize.height
    self.webView.frame.size.height = self.webView.scrollView.contentSize.height
}

但没有运气。它不会改变UIScrollViewUIWebView 的高度值。

我该怎么做?

【问题讨论】:

  • 确保 webview 和 scrollview 的 clipsToBounds 都为 YES

标签: ios swift uiscrollview uiwebview


【解决方案1】:

您可以尝试 layoutSubview,如下所示: [self.scrollView layoutSubview]; [self.webView layoutSubview];

【讨论】:

    【解决方案2】:

    要更新框架或高度/宽度 - 就此而言,您始终首先将translatesAutoresizingMaskIntoConstraints 设置为false,然后根据要求设置框架。如果这不起作用,请在viewWillLayoutSubviews() 中进行更改。一个示例如下所示:

    override func viewWillLayoutSubviews() {
            super.viewWillLayoutSubviews()
            let width = UIScreen.mainScreen().bounds.width
            myView.translatesAutoresizingMaskIntoConstraints = false
            myView.contentSize = CGSizeMake(width, self.mySecondView.frame.height * 4)
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-27
      • 2018-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多