It appears that using sizeToFit() on a UIWebView does not change the size to be smaller, only larger. This is some set-up code:
var scrollView: UIScrollView!
var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
scrollView = UIScrollView()
scrollView.frame = CGRect(x: 50.0, y: 50.0, width: 800.0, height: 1200.0)
webView = UIWebView()
webView.frame = CGRect(x: 50.0, y: 50.0, width: 800.0, height: 1.0)
webView.scrollView.scrollEnabled = false
webView.delegate = self
webView.scalesPageToFit = false
webView.loadHTMLString("Test", baseURL: nil)
self.view.addSubview(scrollView)
scrollView.addSubview(webView)
}
This is my test code. It changes the size of the content to be VERY LARGE after 3 seconds, then VERY SMALL after another 3 seconds:
dispatch_after_delay_main(3) {
self.webView.loadHTMLString("Test Test Test Test ", baseURL: nil)
// Much longer string abbreviated here
dispatch_after_delay_main(3, block: {
self.webView.loadHTMLString("Test", baseURL: nil)
})
}
This is called after the HTML is loaded. The goal is to change the size of the webView based on the size of its content. Note: I've done this with both .sizeToFit() and sizeThatFits() with the same size result.
func webViewDidFinishLoad(webView: UIWebView) {
webView.frame.size.height = 0.0 // Just in case
webView.sizeToFit()
}
I've omitted the print statements at the end of webViewDidFinishLoad(webView: UIWebView) but they output the size growing, but not decreasing. In other words, it will grow, but then once "Test" is loaded, it does not decrease in size.
(800.0, 1.0) // "Test" (before sizeToFit() called)
(800.0, 1976.0) // "Test Test .... Test "
(800.0, 1976.0) // "Test"
do you need the webview embedded on a scrollView? I think not.
Try to change your HTML string to "<div id='main-html'>Your text</div>" and then on the webViewDidFinishLoad delegate function you will do these things:
First, you get the content height with:
var result : Float = NSString (webView.stringByEvaluatingJavaScriptFromString("document.getElementById('main-html').offsetHeight").floatValue
Second, change the webview height with the result value.
And tries to change the height of webView.scrollView.contentSize using the same result value.
Finally, use the webView.sizeToFit()
Cheers
EDIT:
As you said, you can use:
func webViewDidFinishLoad(webView: UIWebView) {
webView.frame.size.height = 1.0 // Value needs to be > 0.0 and < of the current height
webView.sizeToFit()
}
Related
So here's one I just can't seem to find a matching case for in searching on here.
I have a small UIView that contains a UITextView, and the UIView needs to auto-size around the TextView for presentation over another view. Basically the TextView needs to fully fill the UIView, and the UIView should only be big enough to contain the TextView.
The TextView just contains a couple sentences that are meant to stay on the screen until an external thing happens, and certain values change.
Everything is great when I used a fixed-size font.
But hey... I'm an old guy, and I have the text size jacked up a bit on my phone. Testing it on my device shows where I must be missing something.
When using the dynamic font style "Title 2" in the textview properties, and turning on "Automatically adjust font" in the TextView properties, and having the text larger than the default, it seems as if I'm not properly capturing the size of the TextView's growth (with the bigger text) when creating the new bounding rect to toss at the frame. It's returning values that look a lot like the smaller, default-size text values rather than the increased text size.
Code is below, the view's class code as well as the calling code (made super explicit for posting here). I figure I'm either missing something silly like capturing the size after something happens to the fonts, but even moving this code to a new function and explicitly calling it after the controls fully draw doesn't seem to do it.
I hope this make sense.
Thanks, all.
Calling code:
let noWView:NoWitnessesYetView = (Bundle.main.loadNibNamed("NoWitnessesYetView", owner: nil, options: nil)!.first as! NoWitnessesYetView)
//if nil != noWView {
let leftGutter:CGFloat = 20.0
let bottomGutter:CGFloat = 24.0
let newWidth = self.view.frame.width - ( leftGutter + leftGutter )
let newTop = (eventMap.frame.minY + eventMap.frame.height) - ( noWView.frame.height + bottomGutter ) // I suspect here is the issue
// I suspect that loading without drawing is maybe not allowing
// the fonts to properly draw and the
// TextView to figure out the size...?
noWView.frame = CGRect(x: 20, y: newTop, width: newWidth, height: noWView.frame.height)
self.view.addSubview(noWView)
//}
Class code:
import UIKit
class NoWitnessesYetView: UIView {
#IBOutlet weak var textView: EyeneedRoundedTextView!
override func draw(_ rect: CGRect) {
let newWidth = self.frame.width
// form up a dummy size just to get the proper height for the popup
let workingSize:CGSize = self.textView.sizeThatFits(CGSize(width: newWidth, height: CGFloat(MAXFLOAT)))
// then build the real newSize value
let newSize = CGSize(width: newWidth, height: workingSize.height)
textView.frame.size = newSize
self.textView.isHidden = false
}
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.clear // .blue
self.layer.cornerRadius = 10
}
}
This perfect way to do it the content comes from : https://www.youtube.com/watch?v=0Jb29c22xu8 .
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let's create our text view
let textView = UITextView()
textView.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
textView.backgroundColor = .lightGray
textView.text = "Here is some default text that we want to show and it might be a couple of lines that are word wrapped"
view.addSubview(textView)
// use auto layout to set my textview frame...kinda
textView.translatesAutoresizingMaskIntoConstraints = false
[
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
textView.heightAnchor.constraint(equalToConstant: 50)
].forEach{ $0.isActive = true }
textView.font = UIFont.preferredFont(forTextStyle: .headline)
textView.delegate = self
textView.isScrollEnabled = false
textViewDidChange(textView)
}
}
extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: view.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
}
I request a Website with this code
let requestObj = NSURLRequest(URL: url)
myWebView.loadRequest(requestObj)
print (myWebView.scrollView.contentSize.height) //1
print (myWebView.frame.size.height) //2
The code always return 1000.0 when the real size of the website is much more than that. Is there a way to get the real size ? I want to show the hole content of a WebView without the need of scrolling within the WebView.
You need to use the webViewDidFinishLoad delegate method, this answer was originally founded here How to determine the content size of a UIWebView?
In your viewDidLoad method do something like this
override func viewDidLoad() {
super.viewDidLoad()
self.webView.loadHTMLString(component.html, baseURL: nil)
self.webView.scrollView.isScrollEnabled = false
self.webView.scrollView.bounces = false
self.webView.delegate = self
self.delegate = delegate
}
public func webViewDidFinishLoad(_ webView: UIWebView)
{
var frame = webView.frame
frame.size.height = 1
webView.frame = frame
let fittingSize = webView.sizeThatFits(CGSize(width: 0, height: 0))
frame.size = fittingSize
webView.frame = frame
webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.delegate?.heightHasChanged(indexPath:self.indexPath, newHeight: frame.height)
self.loadingView.isHidden = true
self.loadingActivityIndicator.stopAnimating()
}
I hope this helps you, best regards
I wrote following code to fit UITextView's height to its text.
The size changes but top margin relative to first line of text differ every other time when I tap enter key on keyboard to add new line.
Setting
xCode 7.3
Deployment target: iOS 9
import UIKit
class ViewController: UIViewController, UITextViewDelegate {
lazy var textView: UITextView = {
let tv = UITextView(frame: CGRectMake(20, 200, (self.view.frame.width - 40), 0) )
tv.backgroundColor = UIColor.lightGrayColor()
return tv
}()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview( textView )
textView.delegate = self
let height = self.height(textView)
let frame = CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.width, height)
textView.frame = frame
}
func textViewDidChange(textView: UITextView) {
let frame = CGRect(x: textView.frame.origin.x, y: textView.frame.origin.y, width: textView.frame.width, height: height(textView) )
textView.frame = frame
}
func height(textView: UITextView) -> CGFloat {
let size = CGSizeMake(textView.frame.size.width, CGFloat.max)
let height = textView.sizeThatFits(size).height
return height
}
}
I tried few other ways to fit UITextView height but they just acted the same say.
To fix this, subclass UITextView and override setContentOffset to allow scrolling only if the content height is larger than the intrinsic content height. Something like:
override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
let allowScrolling = (contentSize.height > intrinsicContentSize.height)
if allowScrolling {
super.setContentOffset(contentOffset, animated: animated)
}
}
For auto-growing dynamic height text view, the caret moves on starting a new line. At this moment, the size of the text view hasn't grown to the new size yet. The text view tries to make the caret visible by scrolling the text content which is unnecessary.
You may also need to override intrinsicContentSize too.
I'm trying to add a little bit of extra height to the content of a UIScrollView that is within a WKWebView after it loads by adjusting the contentSize property.
I can modify this property, but it somehow keeps changing back to its original size by the time the next layout/display refresh hits.
To test this even further, I attempted to change contentSize in scrollViewDidScroll. Whenever you scroll to the bottom, you can see for a fraction of a second that it's trying add the extra space and keeps reverting back.
I can't reproduce this issue with UIWebView. It works just fine there. Perhaps some changes were added to WKWebView recently? I'm using Xcode 8 and testing on iOS 9/10.
Given my ineptitude with Dropbox I felt badly so put the attached together to try and help you out. If you change the contentInset property of the WKWebView's scrollView rather than contentSize, this seems to work quite well. I agree with you that while you might be able temporarily to change the content size of the scrollView, it reverts quickly; moreover, there are no delegate methods either for UIScrollView or WKWebView that I can find that you might override to counteract this.
The following sample code has a web page and some buttons that allow you to increase or decrease the top and bottom contentInset and animating you to the appropriate point on the scrollView.
import UIKit
import WebKit
class ViewController: UIViewController {
var webView : WKWebView!
var upButton : UIButton!
var downButton : UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let webFrame = CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: self.view.frame.width - 200, height: self.view.frame.height - 200))
webView = WKWebView(frame: webFrame)
webView.load(URLRequest(url: URL(string: <PUT RELEVANT URL STRING (NB - THAT YOU ARE SURE IS VALID) HERE>)!))
webView.backgroundColor = UIColor.red
webView.scrollView.contentMode = .scaleToFill
self.view.addSubview(webView)
func getButton(_ label: String) -> UIButton {
let b : UIButton = UIButton()
b.setTitle(label, for: .normal)
b.setTitleColor(UIColor.black, for: .normal)
b.layer.borderColor = UIColor.black.cgColor
b.layer.borderWidth = 1.0
return b
}
let upButton = getButton("Up")
let downButton = getButton("Down")
upButton.frame = CGRect(origin: CGPoint(x: 25, y: 25), size: CGSize(width: 50, height: 50))
downButton.frame = CGRect(origin: CGPoint(x: 25, y: 100), size: CGSize(width: 50, height: 50))
upButton.addTarget(self, action: #selector(increaseContentInset), for: .touchUpInside)
downButton.addTarget(self, action: #selector(decreaseContentInset), for: .touchUpInside)
self.view.addSubview(webView)
self.view.addSubview(upButton)
self.view.addSubview(downButton)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func increaseContentInset() -> Void {
guard let _ = webView else { return }
webView.scrollView.contentInset = UIEdgeInsetsMake(webView.scrollView.contentInset.top + 100, 0, webView.scrollView.contentInset.bottom + 100, 0)
webView.scrollView.setContentOffset(CGPoint(x: webView.scrollView.contentInset.left, y: -1 * webView.scrollView.contentInset.top), animated: true)
}
func decreaseContentInset() -> Void {
guard let _ = webView else { return }
webView.scrollView.contentInset = UIEdgeInsetsMake(webView.scrollView.contentInset.top - 100, 0, webView.scrollView.contentInset.bottom - 100, 0)
webView.scrollView.setContentOffset(CGPoint(x: webView.scrollView.contentInset.left, y: -1 * webView.scrollView.contentInset.top), animated: true)
}
}
I hope that helps. If you need an answer based specifically on setting the content size then let me know, but I think this is the best option.
I added same subviews in the UIWebView's scrollview.When the webview get contentSize the scrollview will scroll to top.Now I'know the event happend before finished(some images may still loading) but after get the text content.What should i do?
This is the code,and to see the bug you need slow down the request(use edge)!! Before load finished scroll the view,then wait.When the request finished you will see the view scroll to top .
class WebViewController: UIViewController {
var mw = UIScreen.mainScreen().bounds.width
var mh = UIScreen.mainScreen().bounds.height
override func viewDidLoad() {
super.viewDidLoad()
var web = UIWebView(frame: CGRect(x: 0, y: 0, width: mw, height: mh))
var scrollview = web.scrollView
var tmp = UIView(frame: CGRect(x: 0, y: 0, width: mw, height: 300))
tmp.backgroundColor = UIColor.redColor()
scrollview.addSubview(tmp)
(scrollview.subviews.first as UIView).frame.origin.y = 300
let requestURL = NSURL(string: "http://stackoverflow.com/questions/27008545/how-to-disabled-uiwebview-auto-scroll-to-top-when-finished-load")
let request = NSURLRequest(URL: requestURL!)
web.loadRequest(request)
self.view.addSubview(web)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
first you need to set the setContentSize: to the actual content size, not the frame size. That is if your content is e.g. 800px high, you should setContentSize:CGSizeMake(width, 800). using this you can set a UIScrollView that it should scroll to show 800px high content. If you set a UIScrollView's content size equal to its frame, no scrolling is performed and no scroll indicators appear .