UIWebView on scrollView resize the height - ios

Ihave a UIWebView on a scroll view. I want to disable scrolling for my web view. It should scroll when the main scroll view scrolling.But my problem is when the webview content is long I cant see the bottom lines. I am resizing the webview like this.
func webViewDidFinishLoad(webView: UIWebView) {
let url = NSBundle.mainBundle().URLForResource("Script", withExtension: "js")!
let javascript = try! String(contentsOfURL: url)
self.webvw.stringByEvaluatingJavaScriptFromString(javascript)
var frame:CGRect=webView.frame
frame.size.height=1
let fittingSize:CGSize = webView.sizeThatFits(CGSizeZero)
frame.size=fittingSize
self.webvw.frame=frame
self.mainScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.webvw.frame.origin.y+self.webvw.frame.size.height)
self.hud.hide(true, afterDelay: 1.0)
}
But still same. Didn't help me that. How can I solvethis pronlem.
Please help me.
Thanks

WebViews have built in scrolling. If you want to scroll through WebView's content, you would not need to use UIScrollView.
However, if you choose to do so, (I would not recommend), then you must set
you UIWebView's instance's scrollEnabled to false.
And then you will have to track you UIScrollView's content offset, and match UIWebView's scroll's content offset to that value. Doesn't seem like a good idea though. You can just scroll webview itself (without having to use UIScrollView).
And to get UIScrollView's content offset, you must override didScroll method of scrollView delegate.

Related

What happens to WKWebView when scrolled out of bounds

I'm using a paged scroll view displaying different web views as pages. The scroll view has set clipsToBounds = false to allow the display of previous and following pages in the scroll view.
When scrolling through the pages, there is one exact moment, where the WKWebViews content becomes completely invisible. This happens exactly at the moment, where the web view leaves the bounds of the scroll view (see below). On some websites, the visible content out of the bounds is different to the actual content.
These problems only occur when using WKWebView instead of UIWebView (Everything works as intended with UIWebView).
Has someone experienced similar issues with the WKWebView and has a solution for them?
EDIT:
There is a now a sample project on Github, where you can experience the bug and play around with it:
WKWebView test project
To figure it out i think you need to use UICollectionView.
The cell should exist with WKWebView while it will be on screen.
1) You need to place it like Here
And next problem will be to make content pageble.
2) To do it right you need to add UIScrollView over your collection like Here
3) After, make your UIScrollView pageble and write in viewdidload:
collectionView.addGestureRecognizer(scrollView.panGestureRecognizer)
4) After you receive models for showing in UICOllectionView - update your contentSize of UIScrollView like
scrollView.contentSize = CGSize(width: (models.count*(cellWidth+cellIndent) + sectionIndent * 2), height: cellHeight)
5) Implement UIScrollViewDelegate
extension YourVC: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView == self.scrollView {
collectionView.contentOffset = scrollView.contentOffset
} else if scrollView == self.collectionView {
self.scrollView.contentOffset = scrollView.contentOffset
}
}
}
Thats it. Happy coding =)
WKWebView is designed to be more performant than UIWebView. It will stop running some parts of a webpage or stop rendering it completely if it is not on top of the view hierarchy. The problem here is that once the webpage is out of the bounds of the scrollview, WKWebView does not consider itself to be on screen even though it is visible by virtue of setting clipsToBounds = false.
The easiest thing to do would be to use UIWebView. If for some reason you need WebKit, consider adjusting you paging code so your scrollview spans the entire view controller.

WKWebview goes blank after device rotation

I'm making a scrollview with multiple subviews, includes collectionView, webView, and normal UIview. As far as Ive read through, it is not recommended to put webview inside a scrollview. But it is still ok to add webview as subview inside the scrollview.
The requirement is that the webview will dynamically load the content onClick. So, at first, there was a bit of issue trying to dynamically resize the height of the webview as per the content size. The height gets added extra pixels after every rotation. But somehow managed to make it work using webview.sizeToFit() in the viewDidLayoutSubviews().
But now the problem is that, the content of the webview disappears after every 2 rotation of the device. The content should still be there because the webview's frame size didnt change after roation, just the texts inside disappear.
There is not other error as to why the webview is showing blank for after rotated.This is my viewdidlayoutsubview.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.sizeToFit()
}
Not sure how to solve.
I will just answer myself here, since the solution last time was still working well for this. Refresh the view layouts on in the override func viewWillTransition. As per apple's doc, "UIKit calls this method before changing the size of a presented view controller's view". So Ive refreshed all the subview's before changing the orientation.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator){
super.viewWillTransition(to: size, with: coordinator)
//code to refresh the views
}
Just add this line below wkwebview code
i named var = webView so,
webView.autoresizingmask = [.flexiblewidth,.flexibleheight]
hope it works.

How to show html in textview with dynamic height?

My problem is that I have done to show html in textview but problem appear in this that textview height not perfectly set and also try with web view same problem appear.
How to render text in textview with dynamic height ?
Please help me out.
Try using a UIWebView instead.
Implement the UIWebViewDelegate, where webViewDidFinishLoad should look like this:
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(CGSize.zero)
//layout any other elements in the view if needed
}
Do not forget to set the webview's delegate.

UITextView content offset changes after setting frame

I'm building a view that's very similar to the messages app - I have a subview at the bottom of the page with a UITextView in it and as the user types and reaches the end of the line the text view as well as the view containing it should expand upward.
The way I have it working is that in the textViewDidChange: method I call my layout function, and that does
CGFloat textViewWidth = 200;
CGFloat textViewHeight = [self.textView sizeThatFits:CGSizeMake(textViewWidth, 2000)].height;
[self resizeParentWithTextViewSize:CGSizeMake(textViewWidth, textViewHeight)];
// And then the resize parent method eventually calls
textView.frame = CGRectMake(10, 10, textViewWidth, textViewHeight);
The problem is that when typing at the end of line and the view expands, I end up with an arbitrary contentOffset.y of something like 10.5 on the text view so the text is all shifted up to the top of the view. Weirdly, it's alternating on every other line, so expanding the first time leaves the y content offset shifted up, then at the next line it's close to zero, then back to 10.5 on the next line, etc. (not sure if that's helpful or just a strange artifact of my values). I can set it back to zero afterwards but it looks terrible because there's a brief flash where the text has the offset value and then it gets shifted back to the middle.
I've read that it's usually better to use content insets for scroll views rather than changing the frame, but I don't get how to do that because I do need to change the frame size as well.
How can I resize the UITextView without this happening? I think I can get by with setting the text view not to be scrollable and that fixes the issue, but I'd like to understand what's going on.
The problem is that UITextView's scroll animation and your frame setting action were happened at the same time.
UITextView internally scrolls the texts you currently typing to visible when typed one more character at the end of the line or typed the new line character. But the scroll animation does not need because you are expanding the textview. Unfortunately we can't control textview's internal scroll action so the text scrolls to the top of the expanded textview weirdly. And that weird scroll makes unnecessary bottom padding too.
You can avoid this weird action very simply with overriding UITextView's setContentOffset:animated: like this.
Objective-C
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated {
[super setContentOffset:contentOffset animated:NO];
}
Swift
override func setContentOffset(_ contentOffset: CGPoint, animated: Bool) {
super.setContentOffset(contentOffset, animated: false)
}
This code avoids the auto sizing UITextView's unnecessary scroll animations and you can expand the size of the text view freely.
Setting textView.scrollable = NO lets me resize the text view without any strange offsets, that's the only way I've been able to figure out. And I guess it's not too much of a limitation for common scenarios, if you want the text view to be scrollable you probably don't need to resize it on the fly since the user can scroll around as the content changes.
I confronted the same issue: changing the UITextView's frame to fit its content had a side effect on the scroll position being wrong. The UITextView scrolled even when the contentSize was fitting the bounds.
I ended up with setting scrollEnabled to true and with rolling the content offset back if the UITextView is not actually scrollable
override var contentOffset: CGPoint {
didSet {
if iOS8 {
// here the contentOffset may become non zero whereas it shouldn't be
if !isContentScrollable && contentOffset.y > 0 {
contentOffset.y = 0
}
}
}
}
var isContentScrollable: Bool {
let result = ceil(contentSize.height) > ceil(height)
return result
}
Actually, I faced the same issue and found that actually this happens only when UITextView has any Autolayout constraints. If you will try to use UITextView without applying any Constraint then this will not happen and will work fine. but, as you apply Auto layout constraints it automatically scrolls to bottom. To deal with this I just add method
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
self.textView.contentOffset = CGPointZero;
}

Prevent UIWebView to scroll when keyboard appears

I have a UIWebView presenting a page containing input fields ().
When the user touches the field to enter data, the UIWebView is scrolled up and left to make the field stay visible for the user. Well ok.
I don't mind it is scrolled up (because everything below my form is hidden by the keyboard) but I would like to prevent the view from being scrolled left.
Is there a way ( headers in the html page, or iOS code) to prevent this behavior ?
Thanks
not beautiful but effective:
If you don't want your webView to scroll at all, you can implement the UIWebView's UIScrollViewDelegate and set the contentOffset to 0 on scrollViewDidScroll
aWebView.scrollView.delegate = self
func scrollViewDidScroll(scrollView: UIScrollView) {
scrollView.contentOffset = CGPointMake(0, 0)
}
I don't know how to prevent the scrolling entirely, but if you don't mind a little jitter, you can add this handler to the input field:
onfocus="setTimeout('window.scroll(0,0)',100)"
First you will have to capture the display of a Keyboard using UIKeyboardWillShowNotification .
Then restrict the contentSize width of the UIScrollView (of the UIWebview) to screen width which will ensure that the screen doesnot scroll horizontally.

Resources