How to change the height of webView dynamically?

前端 未结 4 1126
無奈伤痛
無奈伤痛 2021-01-16 10:22

I\'m currently using webView to display news,below the news I intend to put some buttons.so I need to get the height of webView so as to change the position of buttons accor

4条回答
  •  悲哀的现实
    2021-01-16 11:03

    The webView has method sizeThatFits: which returns the size of content once the webView is added in a view hierarchy and finished loading the html.

    But this does not work without a caveat. You need to set the frame of webview to a low value before calling the fitting size method.

    For eg: If you have a fixed width and variable height

    //Set a very low height
    CGRect webViewFrame = self.webView.frame;
    webViewFrame.size.height = 5.0f;
    self.webView.frame = webViewFrame;
    //Re calculate the size that fits
    CGSize size = [self.webView sizeThatFits:webViewFrame.size];
    self.webViewFrame.size = size;
    self.webView.frame = webViewFrame;
    

    PS: I want to give credit to the original work. I'm still searching in SO

提交回复
热议问题