inserting various views into UIScrollView - iphone

we're trying to implement an article detail view for an RSS-like application. The windows has a UIScrollView that contains a UITextView (which is the title), a UIButton (which opens a gallery), and a UIWebView (with the body). The idea is that all these elements scroll together...
The problem is that whenever we have a body over 1000px tall, everything below that mark is truncated. We've heard that this is because that is the maximum height for a view, and since the Scroll is handled by the top UIScrollView, there is no way to see what's further down.
does anybody know a way around this?
Thanks!
---- update 1 ----
We've broken up the body into 900px-high chunks, but all webviews after the first one show no content...
UIScrollView *scroll=[[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
[scroll setBackgroundColor:[UIColor whiteColor]];
[scroll setCanCancelContentTouches:NO];
scroll.clipsToBounds = NO;
scroll.indicatorStyle = UIScrollViewIndicatorStyleBlack;
//Título
CGRect tit =CGRectMake(5.0f, 0.0f, 315.f, 50.0f);
UILabel *titulo=[[UILabel alloc] initWithFrame:tit];
titulo.text=[itemNew objectForKey:#"titulo"];
titulo.numberOfLines = 2;
titulo.font=[UIFont fontWithName:#"Helvetica" size:16.0f];
titulo.textColor=[self getColor:#"00336E"];
[scroll addSubview:titulo];
[titulo release];
//Entradilla
float altura_entradilla=calcLabel([itemNew objectForKey:#"entradilla"], [UIFont boldSystemFontOfSize:16], 50, 315.0f);
CGRect ent_frame = CGRectMake(5.0f, 50.0f, 315.0f,altura_entradilla );
UILabel *entradilla=[[UILabel alloc] initWithFrame:ent_frame];
entradilla.text=[itemNew objectForKey:#"entradilla"];
entradilla.numberOfLines=4;
entradilla.font=[UIFont fontWithName:#"Helvetica" size:17.0f];
entradilla.textColor=[UIColor blackColor];
[scroll addSubview:entradilla];
[entradilla release];
NSString *cuerpo=[itemNew objectForKey:#"cuerpo"];
[scroll setContentSize:CGSizeMake(320.0f,40000.0f)];
[scroll setScrollEnabled:YES];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(5.0f, altura_entradilla+50, 315.0f, 900)];
[webView loadHTMLString:cuerpo baseURL: [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
webView.detectsPhoneNumbers=NO;
webView.delegate=self;
[webView setScalesPageToFit:NO];
[scroll addSubview:webView];
webView.backgroundColor=[UIColor blackColor];
webView.userInteractionEnabled=NO;
[webView release];
if (altura_webview>900) {
UIWebView *webView2 = [[UIWebView alloc] initWithFrame:CGRectMake(5.0f, altura_entradilla+50+900, 315.0f, 900)];
[webView2 loadHTMLString:cuerpo baseURL: [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
webView2.detectsPhoneNumbers=NO;
webView2.delegate=self;
[webView2 setScalesPageToFit:NO];
[scroll addSubview:webView2];
webView2.backgroundColor=[UIColor yellowColor];
webView2.userInteractionEnabled=NO;
[webView2 release];
}
self.view=scroll;

check the contentSize on your UIScrollview.
I've not heard of a 1000 point limit (or seen it in bigger views I have in our app).

Use more then one UIWebViews as body
Are you using loadHTMLString to add content to webView?
Try loadRequest method. There is no problem with 1024px when you are using loadRequest

I had the same problem and I read and tried a few tips of this thread, but neither loading the content via loadHTMLString, nor using [_webView setNeedsDisplay] did changed anything for me. (I should try loading more UIWebViews, but I'm lazy:)
I used a quick and dirty fix using the scrollview delegate, which did the trick:
#interface ArticleViewController : UIViewController <UIScrollViewDelegate>
then fire up the view in init
_scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view = _scrollView;
_scrollView.delegate = self;
and use the delgate's method to redraw the webView as it shows up
- (void) scrollViewDidEndDragging: (UIScrollView *) scrollView willDecelerate: (BOOL) willDecelerate
{
CGRect frame = _webView.frame;
frame.size = CGSizeMake(frame.size.width, frame.size.height + 1);
_webView.frame = frame;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
CGRect frame = _webView.frame;
frame.size = CGSizeMake(frame.size.width, frame.size.height + 1);
_webView.frame = frame;
}

When you say :
We've tried breaking the body into several 900px-high webViews, but the second, third, etc. get blanked out (you can see the backgroundColor, but not the content)...
It seems to me that other persons have met this issue (I can't find the posts again), in fact UIWebviews seems to use only one HTML Interpreter. Meaning that if one UIWebView is loading HTML Content, others UIWebView won't load anything.
To solve that, you need to use UIWebView's delegate methods : didFinishLoad, and start loading the other webviews after that.
Please be aware that I'm not sure about what I'm saying, because I did not try it myself, but it might be a solution.

The post marked as the answer has a link in it's last comment. That is the solution to this problem. I had this very same problem, and invoking [[webView _documentView] _webCoreNeedsDisplay]; in those 2 delegates solved it. Cheers.

Related

UIWebview scalesPageToFit doesn't work perfectly

I'm using a small UIWebView, and scalesPageToFit doesn't work correctly for some url. (look like it cannot resize at 100%. (but if i zoom out the webview manually(gesture), it work!
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 400.0f)];
webViewT = [[UIWebView alloc] initWithFrame:view.bounds];
webViewT.delegate = self;
webViewT.scalesPageToFit = YES;
NSURL* url = [NSURL URLWithString:#"http://www.google.com/"];
[webViewT loadRequest:[NSURLRequest requestWithURL:url]];
Please note that i've found a solution:(below work, BUT I have the time to see a "resize"
- (void) webViewDidFinishLoad:(UIWebView *)webViewT {
if ([theWebView respondsToSelector:#selector(scrollView)])
{
UIScrollView *scroll=[theWebView scrollView];
float zoom=theWebView.bounds.size.width/scroll.contentSize.width;
[scroll setZoomScale:zoom animated:NO];
}
}
Any idea? Thanks!
In Some of cases this types of problem Generated. At that time java script use for control of Zoom of UIWebView, use following code.
NSString *jsCommand = [NSString stringWithFormat:#"document.body.style.zoom = 1.5;"];
[webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
Other way is
Refer this Answer UIWebView does not scale content to fit

programmatically added tablview's scroll disabled

I added tableview to scrollview programmatically but it is not scrolling. While i tried the same using XIB. but it is working fine.
here is my code
CGRect frame = CGRectMake(30, 30.0f, 900, 300.0f);
eappTable = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
eappTable.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
eappTable.delegate = self;
eappTable.dataSource = self;
eappTable.scrollEnabled = YES;
eappTable.scrollsToTop = YES;
[eappTable reloadData];
[scrollView addSubview:eappTable];
eappTable = [[UITableView alloc] nitWithFrame:YourSize style:Table_Style]];
eappTable.delegate = self;
eappTable.dataSource = self;
eappTable.scrollEnabled = YES;
[scrollView addSubview:eappTable];
And set self.scrollView.contentSize
You have to set the Content Size of ScrollView.
Hope it Helps !!!
Try to enable bouncing (because if all cells size are smaller than the uitableview size it self, it won't scroll if bounce is set to NO) :
eappTable.bounces = YES;
[eappTable setContentSize:CGSizeMake(320, 680)];
I know this is not the answer you are looking for. But I would like to mention one point :
Never add tableview as the subview of your scrollview.
Important: You should not embed UIWebView or UITableView objects
in UIScrollView objects. If you do so, unexpected behavior can
result because touch events for the two objects can be mixed up and
wrongly handled.
From UIWebView Class Reference

Frame of UIWebView inside UIScrollView does not stretch size to fit all of contents

In a small app that is loading web content from a blog, the UIWebView (which is inside a UIScrollView to be scrollable) will not scroll all of the content into view if it is longer than the actual UIScrollView's frame. Here is an image of the problem:
http://i.imgur.com/G4G4C.png
The code that loads the UIScrollView and the UIWebView (this is part of a UIViewController)
- (id)initWithHTML:(NSString*)html
{
self = [super init];
if (self) {
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
UIScrollView* v = [[UIScrollView alloc] initWithFrame:appFrame];
self.view = v;
UIWebView* webView = [[UIWebView alloc] initWithFrame:v.frame];
webView.userInteractionEnabled = NO;
webView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:webView];
[webView loadHTMLString:html baseURL:nil];
CGSize sz = v.bounds.size;
sz.height = webView.bounds.size.height + 20;
v.contentSize = sz;
[v release];
[webView release];
}
return self;
}
From Apple's documentation:
Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.
It could be that when you are scrolling the events are getting mixed up. This could be the reason why its preventing you from scrolling the rest of the content into view.

Changing background color on UIScrollView?

How do I change the black/gray color to white?
This is just a simple view with a UIView attached to the UIViewControllers property together a with a webview that fills the UIView.
UPDATE
Here's the code that works:
- (void)loadView {
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 416.0f)];
[webView setBackgroundColor:[UIColor whiteColor]];
self.view = webView;
[webview release];
}
Thanks in advance.
You can use the UIScrollView's backgroundColor property to achieve this, the signature being:
#property(nonatomic, copy) UIColor *backgroundColor
As such to change it to white, you'd use:
[myScrollView setBackgroundColor:[UIColor whiteColor]];
A few improvements for your code:
- (void)viewDidLoad {
[super viewDidLoad];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(self.view.bounds)];
[webView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview: webView];
[webview release], webview = nil;
}
Overall you should find this approach less brittle.
PS. If you keep a reference to the UIWebView around, don't forget to release it in - viewDidUnload.

Anti-aliasing not working when resizing a UIWebView

I'd like to add a Web View to my app at 60% scale (like seen in Safari in the browse other windows view):
Notice how the content looks nice and Aliased!
If I try and add the same Web view to my app:
NSURL *url = [NSURL URLWithString:#"http://www.google.co.uk?q=hello"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
webView.delegate=self;
[webView loadRequest:request];
[self.view addSubview:webView];
Using the following transformation:
[webView setTransform:CGAffineTransformMakeScale(0.6, 0.6)];
..the scale is really bad quality and there appears to be no anti-aliasing.
Does anyone know why this is happening or have a suggestion on how it could be fixed?
Thanks!
Nick.
I figured out a solution!
Take a snapshot of the webview, add it to a UIImageView and resize that insted!
UIGraphicsBeginImageContext(webView.bounds.size);
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(webView.bounds.x, webView.bounds.y, webView.frame.size.width, webView.size.height)];
imageView.image = viewImage;
[self addSubview:imageView];
[imageView release];
webView.hidden = YES;
Seems to work fine!
One point to note.. if you want to do this as soon as the WebView has loaded, you need to defer the action by some period (I chose a 10th of a second!)
Like so:
-(void)deferLoading
{
webViewHasActuallyLoaded = YES;
[self setNeedsLayout];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[NSTimer scheduledTimerWithTimeInterval: 1.0/10.0 target:self selector:#selector(deferLoading) userInfo:nil repeats:NO];
}
Cheers for suggestions abovee!
Nick.
Why not just make the web view smaller? It should render the content in high quality at the smaller size.
If you need to animate the change in size, you can set the minificationFilter on the web view's layer to get reasonable quality:
[[webView layer] setMinificationFilter:kCAFilterLinear];
In your UIViewController override
(void)placeViewItems: (UIInterfaceOrientation)interfaceOrientation
And make your UIWebView's frame.origin.x & frame.origin.y integer values.
See if that is the result you are looking for.