URL is not loading completely on my UIWebView in iOS? - ios

I am working on an iPhone application, where i am opening an URL to my UIWebView. The problem is URL is not loading perfectly. As per my requirement, i need to add UIWebView as footer view of my table. but the problem is after getting the web view content height size from webViewDidFinishLoad method, i am setting frame of my footer view. here is my code :
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
aWebView.scrollView.scrollEnabled = NO;
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
[self setFooterView:fittingSize.height + aWebView.frame.origin.y];
}
-(void)setFooterView:(float)fittingHeight
{
bottomView.frame = CGRectMake(0, 0, 320, fittingHeight);
webViewSocial.frame = bottomView.frame;
[tableView reloadData];
}
But when i am scrolling my table view, then my UIWebView (footer view) is not scrolling completely. Can you please tell me how can i achieve my output. Thanks!

try to set like this
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[self.webView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = self.webView.frame;
frame.size.height = height;
frame.size.width = width;
self.webView.frame = frame;
self.tableView.tableFooterView = webView;
}

Related

determine UIWebView height when web view content resized

Am using UIWebView to load a widget in my view controller I want to adjust the frame when widget content size is changed.
My below code returns the original height when an option is selected twice
Here is my code
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
CGRect frame1 = _widget_VW.frame;
frame1.size.height = 60;
_widget_VW.frame = frame1;
[_widget_VW sizeToFit];
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(#"User tapped a link.");
[_widget_VW sizeToFit];
[_widget_VW setSuppressesIncrementalRendering:YES];
CGRect frame = _widget_VW.frame;
frame.size.height = 1;
_widget_VW.frame = frame;
CGSize fittingSize = [_widget_VW sizeThatFits:CGSizeZero];
frame.size = fittingSize;
_widget_VW.frame = frame;
_VW_activity.hidden = NO;
[_activityindicator startAnimating];
[self performSelector:#selector(setup_DATA) withObject:_activityindicator afterDelay:0.01];
return YES;
}
return YES;
}
Use this.
It worked for me.
#pragma mark - UIWebViewDelegate methods
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
}

UIWebView with dynamic height inside a UIScrollView in Objective-C

I am trying to make a UIWebView with "dynamic screen height". This webview is inside a UIScrollView, below other components. See the layout (it uses autolayout):
My idea is to provide just one scroll (from UIScrollView - its working) and make the WebView viewport grow depending the content size. And its not working.
What I did to try to do this:
UIWebView property "scale page to fit " is on;
UIWebView is unpaginated;
UIWebView does not allow user interaction.
In my UIViewController:
- (void) viewDidAppear:(BOOL)animated {
NSString *urlString = #"MY URL GOES HERE";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[_webViewDescription loadRequest:urlRequest];
_webViewDescription.delegate = self;
_webViewDescription.scrollView.scrollEnabled = NO;
_webViewDescription.scrollView.bounces = NO;
}
My UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
frame.size.height = 1;
webView.frame = frame;
CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
_myScrollView.contentSize = webView.bounds.size;
}
When I run the code, it prints the correct UIScrollView size and UIWebView size. The UIScrollView height got bigger, but the UIWebView maintains the same height from the first loading.
I am testing in a real device.
add a height constraint to your webView and make a IBOutlet of that like
#property(strong, nonatomic) IBOutlet NSLayoutConstraint *webViewHeightConstraint;
load your webview and in web view delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
get webview content height and set webViewHeightConstraint.constant
like below:-
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *str = [webView stringByEvaluatingJavaScriptFromString:#"(document.height !== undefined) ? document.height : document.body.offsetHeight;"];
CGFloat height = str.floatValue;
webViewHeightConstraint.constant = height;
}
hope it may help you.
Try to set webView.frame = frame; in viewDidLayoutSubviews()
For setting dynamic height for UIWebView, you need to set the height in webViewDidFinishLoadView method
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
//For Width
//CGFloat width = [[webview stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = webview1.frame;
frame.size.height = height;
//frame.size.width = width; //Width
webview.frame = frame;
CGRect screenBounds = [UIScreen mainScreen].bounds ;
CGFloat widthForIpad = CGRectGetWidth(screenBounds) ;
CGFloat heightForIpad = CGRectGetHeight(screenBounds) ;
NSLog(#"The iPad width is - %fl",widthForIpad);
NSLog(#"The iPad height is - %fl",heightForIpad);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([[UIScreen mainScreen] bounds].size.height == 568)
scrollView.contentSize = CGSizeMake(320, height+370); //set your required height
else
scrollView.contentSize = CGSizeMake(320, height+350); //set your required height
}
else
{
if ([[UIScreen mainScreen] bounds].size.height == 1024)
scrollView.contentSize = CGSizeMake(320, height+370); //For iPad
}
}
You can get the content size of webview content using webView.scrollView.contentSize.height.
and than you can use this webView.scrollView.contentSize.height to set the contentSize of the scrollview inside webViewDidFinishLoad method.
Like this in webViewDidFinishLoad method
[objWebView setFrame:CGRectMake(objWebView.frame.origin.x, objWebView.frame.origin.y, objWebView.frame.size.width, webView.scrollView.contentSize.height)];
if(objWebView.frame.origin.y+objWebView.frame.size.height > objScrollView.contentSize.height) {
[objScrollView setContentSize:CGSizeMake(objScrollView.frame.size.width, objWebView.frame.origin.y+objWebView.frame.size.height)];
}

UIScrollview imbeded within UIPageviewController resizes after page turned

So I have a UIPageviewController used that contains various news articles. The articles are presented on UIWebviews of varying lengths, and each UIWebview is contained within a UIScrollview. When the first article is loaded, everything works, and the user is able to scroll up and down the page and read the entire article. However, when the user swipes to the left or right to view the next article, the UIScrollview on that page is resized to only be the height of the screen and thus will only scroll enought to bounce back. I can tell that the UIWebview is longer than what is being shown because I have printed it's height, and when scrolling, you can see that the article continues. Also, when you swipe back to the initial article View, this UIScrollview is also resized to only be the screen size.
The UIWebView has it's content's loaded in the ArticleViewController's init method, and then is sized within the webViewDidLoad method like this:
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
This seems to work, although tell me if you think I should change it.
To get to each article, you click on it from a UITableview presenting each of the articles. Within the DidSelectRowAtIndexPath method for this, I set up the UIPageViewController like so:
UIPageViewController *articlesPageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
articlesPageViewController.dataSource = self;
articlesPageViewController.delegate = self;
NVM! The Solution was turning off auto layout for the UIScrollview! Thanks everyone!
(void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame; NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}
Get height of the webview then set the content size of the scrollview
OR try this way:
CGFloat height = [[myWebView stringByEvaluatingJavaScriptFromString:#"document.height"] floatValue];
CGFloat width = [[myWebView stringByEvaluatingJavaScriptFromString:#"document.width"] floatValue];
CGRect frame = myWebView.frame;
frame.size.height = height + 125.0;
frame.size.width = width;
myWebView.frame = frame;
mainScrollView.contentSize = myWebView.bounds.size;

How can i change both table view cell height and webview height based on webview content?

I have a list of 15 questions each of them again have sub questions, I'm showing these questions on webView which is present in tableViewCell. Below to this webView there are three labels and three buttons. Now problem is that I'm not getting the heights of tableViewCell based on webView's content. Please suggest me a good solution and thanks in advance.
Here is my code in webviewdidfinishload
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
int fontSize =160;
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
[aWebView stringByEvaluatingJavaScriptFromString:jsString];
CGRect frame = aWebView.frame;
frame.size.height = 1;
aWebView.frame = frame;
CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
aWebView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}

UIWebView ContentSize Height

I have a UIImage, a UILabel and a UIWebView inside a UIScrollView. Basically, I'm calculating the height of both the UIImage and the UILabel and adding it to the UIWebView height to set the UIScrollView's contentSize.
Here's how I calculate the UIWebView height:
- (void) webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame = webView.frame;
CGSize fittingSize = [webView sizeThatFits:webView.scrollView.contentSize];
frame.size = fittingSize;
webView.frame = frame;
self.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, frame.size.height + y + 10);
[webView setFrame:CGRectMake(x-8, y, webView.frame.size.width, frame.size.height)];
[webView setContentMode:UIViewContentModeScaleAspectFit];
}
The problem is for some cases the UIWebView is cut on the bottom... Any ideia why?
This might be relevant: the UIWebView content is HTML code I have no control on.
try out the following code:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView {
CGRect frame = WebView.frame;
frame.size.height = 1;
WebView.frame = frame;
CGSize fittingSize = [WebView sizeThatFits:CGSizeZero];
frame.size = fittingSize;
WebView.frame = frame;
NSLog(#"size: %f, %f", fittingSize.width, fittingSize.height);
}

Resources