UIWebView autorotation weird behavior - iphone

I managed to show a UIWebView that shows content from the internet.
But apparently it does not rotate as it should:
http://www.youtube.com/watch?v=VV0BmNkSrvU
I'm using this code:
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window) {
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
}
_webView = [[UIWebView alloc] initWithFrame:rect];
_webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
NSURL* url = [[NSURL alloc] initWithString:URL]; NSURLRequest* request = [[NSURLRequest alloc] initWithURL:url];
[_webView loadRequest:request];
//Unity's UIView
UIView* subView = [window.subviews objectAtIndex:0];
[url release];
[subView addSubview:_webView];

It seems that this code has not been placed inside a UIViewController. Autorotation works correctly only inside a UIViewController. If you want to manage it outside a UIViewController you must observe all orientation change notifications and then transform your view accordingly.

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

UIWebview fitting to screen but not zooming in iphone programming

I am working on uiwebview and it is supposed to load dynamic urls coming from the server.
i want to show it fitting to the iPhone screen size in my iPhone application and also it should zoom in and out.
Code i am using for it is:
m_pView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 430)];
m_pView.backgroundColor = [UIColor blackColor];
self.view = m_pView;
UIWebView *aWebView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 1, 320, 400)] autorelease];
aWebView.dataDetectorTypes = UIDataDetectorTypeNone;
aWebView.delegate = self;
NSString* url = [NSString stringWithFormat: #"http://%#",[m_URL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
aWebView.scalesPageToFit = YES;
aWebView.autoresizesSubviews = YES;
aWebView.frame = self.view.bounds;
NSURL *aURL = [NSURL URLWithString: url];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
//load the index.html file into the web view.
[aWebView loadRequest:aRequest];
[m_pView addSubview:aWebView];
self.m_webView = aWebView;
Please let me know how to achieve webview fitting to the iphone screen size without loosing zoom in/out feature.
Thanks in advance.
Use following code
yourWEbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
yourWEbView.contentMode = UIViewContentModeScaleAspectFit;
problem might be in this line:
aWebView.frame = self.view.bounds;
Why you're setting bounds of self.view while adding webview to m_pView?

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

How to customize UIWebView?

Actually i have one table view ,when Select rowAtIndexPath then it load my webView and play my video, how to return from UIWebView to my tableView ?
-(void)playButtonPressed1:(UIButton*)sender
{
Video *currentVideo= [[xmlParservideo videoNames] objectAtIndex:btn1.tag];
NSLog(#"content============== %#", [currentVideo content2]);
CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlString=currentVideo.content2;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[[self view] addSubview:webView];
// [self.navigationController popViewControllerAnimated:YES];
NSLog(#"jointed2 jjjjjjjjj %d",btn1.tag);
Place the Uiwebview in another view controller and push to it when a row is selected and when the video complete pop back to the tableview controller.
On didSelectRowAtIndexPath
videoViewController *newView = [videoViewController alloc]initWithNibName:#"videoViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
and in your init method of the view controller create the webView programatically
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = #"Video Player";
UIWebView *videoWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 290)];
[videoWebView setDelegate:self];
NSString *urlAddress = #"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[videoWebView loadRequest:requestObj];
[[self view] addSubview:videoWebView];
}
return self;
}
Ok, Now just pop your view after completing your video
[self.navigationController popViewControllerAnimated: NO];
A workaround that may work is using this: [webView setHidden:YES];
This hides the webView from the view, so use that line of code whenever you want the webview to disappear. When you want it to return to the view, use this: [webView setHidden:NO];

Three20 doesn't show website in UIWebView

Hi all
I have a tabbar based application. In one view I have a TTTabStrip with different TTTabItems. Everything works (loading normal UIViewControllers) except loading UIWebViews.
In the app delegate I have set up the url mapping:
TTNavigator* navigator = [TTNavigator navigator];
navigator.supportsShakeToReload = YES;
navigator.persistenceMode = TTNavigatorPersistenceModeAll;
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]]; // default klasse wenn nichts anderes gewählt ist.
[map from:#"tt://test" toViewController:[TestController class]];
The test controller contains one UIWebView. I can see (NSLog) that the loadView-Method gets called, but the URL is not being opened:
- (void) loadView {
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
self.view = [[[UIView alloc] initWithFrame:applicationFrame] autorelease];
//self.view.backgroundColor = [UIColor blackColor];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
// support zooming
webView.scalesPageToFit = YES;
}
For instance, I can change the background of the UIWebView to black.
How can I load google in the web view below the TTTabStrip?
Thanks a lot.
Try the following and add the method viewWillAppear:
- (void)loadView
{
CGRect applicationFrame = [UIScreen mainScreen].applicationFrame;
UIView * newView = [[UIView alloc] initWithFrame:applicationFrame];
// support zooming
webView.scalesPageToFit = YES;
[newView addSubview:webView];
self.view = newView;
[newView release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSURL *theURL = [NSURL URLWithString:#"http://www.google.com"];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
}