Get UIWebView to scroll and bounce horizontally but not vertically - iphone

I have an iphone UIWebView with contents that are approx 1000px x 200px. Currently when scrolling with your finger, it'll bounce vertically but not horizontally.
I want it to do the opposite: bounce horizontally but not vertically.
Is there some trickery i need to do in my html to achieve this? Eg setting the width and height on the body explicitly maybe? Or is it a setting of the UIWebView? I can do the following to totally disable bouncing, is there something similar to set bouncing on a per-direction basis?
[[[webView subviews] lastObject] setBounces:NO];
Thanks a lot for the help

In iOS 5, you don't need to access the subviews. UIWebView contains a scrollView property to the nested UIScrollView:
To disable bounce:
webView.scrollView.bounces = NO;
To only bounce vertically:
webView.scrollView.alwaysBounceVertical = NO;
webView.scrollView.alwaysBounceHorizontal = YES;

You can try to add a CSS StyleSheet and set the a container Div with the dimension of your view. and apply the styleSheet to your UIWebView.
You can do it like that -
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"style" ofType:#"css"];
//do base url for css
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *html =[NSString stringWithFormat:#"<!DOCTYPE html><html><head><link rel=\"stylesheet\" href=\"%#\" type=\"text/css\" /></head><body><section></section><br><br></body></html>",,cssPath];
[webView loadHTMLString:html baseURL:baseURL];

You can cycle through all the UIWebViews subviews(using a for loop) checking each one to see if it's a UIscrollView (using isMemberOfClass:) and then change the properties in the UIScrollView to meet your needs.
for (UIView *subview in [myWebView subviews]) {
if ([subview isMemberOfClass:[UIScrollView class]]) {
[(UIScrollView *)subview setAlwaysBounceVertical:NO];
[(UIScrollView *)subview setAlwaysBounceHorizontal:YES];
}
}

You can always do:
for (UIView *subview in [self.webView subviews]) {
SEL sel = #selector(setAlwaysBounceHorizontal:);
if( [subview respondsToSelector:sel] ){
[subview performSelector:sel withObject:(id)YES];
}
}
in your viewDidLoad

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

Why Is Youtube Video Not Filling UiWebView?

I'm a noob to iphone development and I am having trouble displaying an embedded youtube video within a UIwebview. Currently, the youtube video sits in the top left hand corner of the uiwebview and it also has an annoying border. I set the youtube frame to the same dimensions as my uiwebview and the webview is set to "scale to fit". How do I make my youtube content fit the entire screen and get rid of the border? Any help is greatly appreciated.
MY CODE
NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"320\" height=\"230\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];
EDIT
NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%\" height=\"100%\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];
Have u tried this......
NSString *video_ID = youtubeid;
NSString *htmlStr = [NSString stringWithFormat:#"<iframe class=\"youtube-player\" type=\"text/html\" width=\"100%%\" height=\"100%%\" src=\"http://www.youtube.com/embed/%#\" frameborder=\"0\"></iframe>",video_ID];
[videoScreen loadHTMLString:htmlStr baseURL:nil];
Note That %% is used for % character.
So first you must create a UIWebView and add it to your main view.
In the following I create a webview pointing to www.example.com where I have embedding the YouTube clip I want to play, I set it hidden because I'll later want to launch the video full screen.`
UIWebView *htmlView;
htmlView = [[UIWebView alloc] initWithFrame:CGRectMake( 55.0f, 105.0f, 369.0f, 178.0f) ];
[htmlView setBackgroundColor:[UIColor blackColor]];
htmlView.hidden = YES;
htmlView.delegate = self;
[self addSubview:htmlView];
[htmlView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"www.example,com"]]]; `
So here I scan through all the buttons in the webview and select the first button found using the sendActionsForControlEvents function.
UIButton* findButtonInView(UIView* view)
{
UIButton *button = nil;
if( [view isMemberOfClass:[UIButton class]] )
{
return (UIButton*)view;
}
if( view.subviews && [view.subviews count] > 0 )
{
for( UIView *subview in view.subviews )
{
button = findButtonInView( subview );
if( button )
{
return button;
}
}
}
return button;
}
-(void)webViewDidFinishLoad:(UIWebView*)webView
{
UIButton *button = findButtonInView( webView );
if( button != nil )
{
[button sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}

how to eliminate the blank frame while flipping over to the next view

i am currently using this(AFKPageFlipper)
to produce the desired flipboard animation in my application
,there are 4 static html pages named 1.html,2..so on
loadView
- (void) loadView {
[super loadView];
self.view.autoresizesSubviews = YES;
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
flipper = [[AFKPageFlipper alloc] initWithFrame:self.view.bounds] ;
flipper.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSURLRequest *urlReq=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"1" ofType:#"html"]isDirectory:NO]];
;
//[web loadRequest:urlReq];
NSURLRequest *urlReq1=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"2" ofType:#"html"]isDirectory:NO]];
NSURLRequest *urlReq2=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"3" ofType:#"html"]isDirectory:NO]];
;
NSURLRequest *urlReq3=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"4" ofType:#"html"]isDirectory:NO]];
arr=[[NSArray alloc]initWithObjects:urlReq,urlReq1,urlReq2,urlReq3,nil];
flipper.dataSource = self;
[self.view addSubview:flipper];
}
data source for the AFKFlipper
- (NSInteger) numberOfPagesForPageFlipper:(AFKPageFlipper *)pageFlipper
{
NSLog(#"%i",arr.count);
return [arr count];
}
- (UIView *) viewForPage:(NSInteger) page inFlipper:(AFKPageFlipper *) pageFlipper
{
web= [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, 320, 385)];
[web loadRequest:[arr objectAtIndex:page-1]];
return web;
}
i can flip across multiple webviews,but the problem i am facing is when i am halfway flipping the page i cant see my next view,it seems to be a blank page,it gets loaded after i flip my page completely
In the flipboard you can see the next view partially and previous view partially,what should i do to show my next view when the current view if halfway flipped
I tried AFKPageFlipper,, but i dont think messing with its original code is any solution.
what u can try is, keep ready all the views u r going to provide to AFKPageFlipper.
NSMutableArray *webViewArray;
-(void)viewDidLoad
{
webViewArray=[[NSMutableArray alloc]int];
for(int ii=0;ii<4;ii++)
{
web= [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, 320, 385)];
[web loadRequest:[arr objectAtIndex:ii]];
[webViewArray addObject:web];
web=nil;
}
}
- (UIView *) viewForPage:(NSInteger) page inFlipper:(AFKPageFlipper *) pageFlipper
{
return [webViewArray objectAtIndex:page];
}

About PDF vertical sliding from page to page in iOS

I am work on an app that can make PDF vertical sliding in iOS using CGPDFDocumentRef.
Now i can display one page PDF perfectly, but I don't know how can make it vertical sliding from page to page.
Can anyone help me? Give me an idea or sample,thank you
Sorry for my poor English.
add UIWebView in WebViewViewController.xib and connect that.Implement same as i have posted.
#interface WebViewViewController : UIViewController<TapDetectingWindowDelegate>
{
}
#property (strong, nonatomic) IBOutlet UIWebView *mWebView;
#end
#implementation WebViewViewController
#synthesize mWebView;
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"EventHandlingiPhoneOS"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[mWebView loadRequest:request];
}
This is the best PDF library you could integrate and implement PDF view horizontally.
https://github.com/vfr/Reader
Why don't you use UIWebView for showing the PDF file?
It is very simple and easy to use it!
However, if you really want to make your custom viewer, you can use UIScrollView for it.
like this:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0,320,640)];
[self.view addSubview:scroll];
view1.frame = CGRectMake(0,0,320,640);
view2.frame = CGRectMake(0,320,320,640);
view3.frame = CGRectMake(0,640,320,640);
[scroll addSubview:view1];
[scroll addSubview:view2];
[scroll addSubview:view3];
scroll.contentSize = CGSizeMake(320, 960);
[scroll release];

inserting various views into UIScrollView

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.