UIScrollview imbeded within UIPageviewController resizes after page turned - ios

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;

Related

Autolayout UITextView inside UIScrollView size not correct

So I know there are already a lot of questions about this subject, but I haven't found one that has solved my problem (or perhaps I don't understand the answer).
Alright, so I have set up a scrollview that has an UIView in it, containing an image view at the top (a gradient view which you can ignore), and a textview under it. The textview has to expand with whatever is put inside of it without scrolling (hence why it's in the scrollView).
I have in code:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
[self setupGradientView];
[self resize];
}
- (void) resize {
CGFloat maxWidth = [[UIScreen mainScreen] bounds].size.width;
CGRect newFrame;
// reset width for scrollview
newFrame = imageView.frame;
newFrame.size = CGSizeMake(maxWidth, imageView.frame.size.height);
imageView.frame = newFrame;
gradientView.frame = newFrame;
newFrame = textView.frame;
newFrame.size = CGSizeMake(maxWidth, textView.frame.size.height);
//textView.frame = newFrame;
newFrame = dummyView.frame;
newFrame.size = CGSizeMake(maxWidth, dummyView.frame.size.height);
dummyView.frame = newFrame;
// resize height
CGFloat fixedWidth = textView.frame.size.width;
CGSize newSize = [textView sizeThatFits:CGSizeMake(fixedWidth, CGFLOAT_MAX)];
newFrame = textView.frame;
newFrame.size = CGSizeMake(fixedWidth, newSize.height);
//textView.frame = newFrame;
self.textViewHeightConstraint.constant = newSize.height;
CGFloat dummyViewHeight = textView.frame.origin.y + textView.frame.size.height;
dummyView.frame = CGRectMake(dummyView.frame.origin.x, dummyView.frame.origin.y, dummyView.frame.size.width, dummyViewHeight);
[scrollView setContentSize:dummyView.frame.size];
}
Unfortunately, the content size seems to be off by what I think may be 16pts, resulting in a view that looks like this with the text cut off (don't mind the unicode mess in the text):
Here's a picture of what the view looks like in IB: https://i.imgur.com/hxzzUg2.png
Here's what the constraint hierarchy looks like: https://i.imgur.com/rUepwa2.png
Any help would be greatly appreciated!
EDIT: If I comment out the entire code in the resize function, I get the same result. It would appear that auto-layout is resizing all of this after the function is called...but I thought auto-layout was completed when viewDidLayoutSubviews was called?
Edit 2: I have tried and found that adjusting the frame of the textView even n viewDidAppear has no effect. (I can edit things such as the background color)
What I ended up having to do was setting equal width and equal height to the view (not the scrollview), and then manually changing the height constraint in viewDidLayoutSubviews, along with changing the frame of the textView and the content size of the scrollView.

URL is not loading completely on my UIWebView in 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;
}

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);
}

Calculate height based on content in webview iOS7

Actually we are using a webview to load RTF which we get by invoking a service.
[self.messageWebView loadData:[somemessage dataUsingEncoding:NSUTF8StringEncoding]
MIMEType:#"text/rtf"
textEncodingName:#"utf-8"
baseURL:nil];
On
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame = webView.frame;
frame.size.height = 1;
frame.size.width = 1;
webView.frame = frame;
webViewSize = [webView sizeThatFits:CGSizeZero];
frame.size = webViewSize;
webView.frame = frame;
}
This is how we are calculating the height of webview based on this How to determine the content size of a UIWebView? But the size I get is not accurate. I also tried the other solution as suggested in the other post based on Scrollview, unfortunately even that solution did not work.
Can someone suggest if there is a better approach to calculate the height based on content.

UIScrollView zooms subviews when increasing subview dimensions

I have a UIScrollView that contains a view derived from UIView that uses CATiledLayer. Essentially, in my ViewController viewDidLoad:
_tiledView = [[TiledView alloc] initWithFrame:rect tileSize:_tileSize];
_scrollView = [[ScrollingView alloc] initWithFrame:rect];
_scrollView.contentSize = _tiledView.frame.size;
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
_scrollView.scrollEnabled = YES;
_scrollView.delegate = self;
[_scrollView addSubview:_tiledView];
Initially, _tiledView is a 4x4 grid of 256x256 tiles. I'm trying increase the dimensions of _tiledView at runtime. When constructing _tiledView, I simply compute the size of the view by multiplying the number of tiles by its size. Then I set the size of _tiledView.frame and _tiledView.bounds, e.g.:
CGRect frame = self.frame;
frame.origin = CGPointZero;
frame.size = CGSizeMake(tileSize.width*4, tileSize.height*4);
self.frame = frame;
self.bounds = frame;
Now, when I hit a button in the interface, all I want to accomplish as a first step is increasing the dimensions of _tiledView by one 256x256 tile to both right and bottom. This is what I attempted:
- (void)addTiles:(id)sender
{
CGRect rect = _tiledView.frame;
rect.size.width += _tileSize.width;
rect.size.height += _tileSize.height;
_tiledView.frame = rect;
_tiledView.bounds = rect;
CGSize size = _scrollView.contentSize;
size.width += _tileSize.width;
size.height += _tileSize.height;
_scrollView.contentSize = size;
[_scrollView setNeedsLayout];
}
However, this doesn't work as expected. What happens is that _tiledView gets bigger as if it had been zoomed in - same number of tiles as the beginning though, i.e. 4x4. I checked the _scrollView.contentsScaleFactor property and it says 1.0. I assume _scrollView did not technically zoom the contents in.
I was expecting _tileView to stay put in its current place in the interface but add 9 new tiles, i.e. 4 to the right, 4 at the bottom and 1 at the bottom-right corner. Instead, the initial 16 tiles got bigger to fill in the space that could have been filled by 25 tiles.
What am I missing? What am I doing wrong? Any help would be appreciated.
In case anyone finds it useful. After digging further, I realized that I the contentMode defaults to ScaleToFill. So I set it to:
_tiledView.contentMode = UIViewContentModeRedraw;
upon initialization. And adjusted addTiles like this:
CGRect rect = _tiledView.frame;
rect.size.width += _tileSize.width;
rect.size.height += _tileSize.height;
_tiledView.frame = rect;
_tiledView.bounds = rect;
_scrollView.contentSize = rect.size;
[_tiledView setNeedsDisplay];
And, that had the effect I was looking for.

Resources