NimbleKit - fast iOS app development

webView and device rotation

Forum for posting sample code and examples, new posts creation is not allowed, but everybody can comment

Re: webView and device rotation

Postby HughJohnson » Thu Aug 11, 2011 8:43 pm

Dilemma Apps wrote:Sorry it doesn't do anything for native controls.

Doh, I was so hoping I was just missing something and it did work.
Image
User avatar
HughJohnson
NimbleKit Expert
 
Posts: 286
Joined: Thu Jun 18, 2009 4:44 pm

Re: webView and device rotation

Postby rue » Thu Aug 11, 2011 9:45 pm

HughJohnson wrote:
rue wrote:... the innerWidth doesn't seem to get updated until after the 1st pageDidRotate() function execution.


Yeah I had the same problem but it fires the page did rotate function when the app starts in landscape, so it fixes it correctly. Is this not the case for you? I only tried on iPad2 so could be different for you.


I haven't tested your code on a real device... just on the Simulator. So I don't know if a real device will not have this problem.

Can someone check this code on a real iPhone???
rue
 
Posts: 49
Joined: Thu Aug 04, 2011 5:00 am

Re: webView and device rotation

Postby cactuscraig » Wed Aug 17, 2011 10:13 pm

what I use is:
Code: Select all
 function pageDidRotate()
    {
        setTimeout("modifyElements()",100);
    }



Because of asynchronicity the window.innerWidth & window.innerHeight are not set right away. By doing a 100ms timeout, gives time for the device to rotate, then fire the function.
Last edited by cactuscraig on Mon Oct 03, 2011 8:00 pm, edited 1 time in total.
User avatar
cactuscraig
 
Posts: 846
Joined: Thu Feb 03, 2011 5:15 pm
Location: Scottsdale, AZ

Re: webView and device rotation

Postby ritchielee » Mon Oct 03, 2011 7:48 pm

Hi
If it's any help to anyone, I've tested on an ipad various connotations of the code posted in this thread and found that although not perfect, a combination works best (see below). You will still get a split view if you rotate main2.html and then go back to main.html, but the user can rectify with a turn.

Code: Select all
function pageWillRotate(degree) { 
              webView.init(0,0,window.innerWidth,window.innerHeight);
           }

function pageDidRotate(degree) {
         setTimeout(modifyElements,100); 
            webView.init(0,0,window.innerWidth,window.innerHeight);

           }
ritchielee
 
Posts: 8
Joined: Wed Sep 28, 2011 3:20 pm

Re: webView and device rotation

Postby cactuscraig » Mon Oct 03, 2011 7:57 pm

What is in your modifyElements function ?
Code: Select all
function modifyElements()
{
   // Do something...
}


And, correcting my previous post, it should be
Code: Select all
setTimeout("modifyElements()",100);
User avatar
cactuscraig
 
Posts: 846
Joined: Thu Feb 03, 2011 5:15 pm
Location: Scottsdale, AZ

Re: webView and device rotation

Postby ritchielee » Mon Oct 03, 2011 10:34 pm

Hi

Thanks for posting: realised that 'function call' & timeout can be omitted for the results I managed.

Did you have anything in there that would help on the last problem - switching back to main after page 2?

ps. thanks for your posts and samples all over the forum which have helped me get to grip with things

R
ritchielee
 
Posts: 8
Joined: Wed Sep 28, 2011 3:20 pm

Re: webView and device rotation

Postby cactuscraig » Mon Oct 03, 2011 11:00 pm

not sure I understand your issue, exactly. Have you tried using the setTimeout function ? I find the iPad doesn't get the results of the window.inner? right away.



Code: Select all
function pageWillRotate(degree) { 
     setTimeout("initWeb()",500);
             
           }

function pageDidRotate(degree) {
         setTimeout("initWeb()",500);

           }

function initWeb()
{
      webView.init(0,0,window.innerWidth,window.innerHeight);
}


User avatar
cactuscraig
 
Posts: 846
Joined: Thu Feb 03, 2011 5:15 pm
Location: Scottsdale, AZ

Re: webView and device rotation

Postby ritchielee » Mon Oct 03, 2011 11:32 pm

I see what you're trying to do, but it only adds a jump to the resizing of the content on rotate.

I'll try and describe the problem better, which you've all mentioned BTW:

If I'm on page one [portrait], then navigate to page two. Then on page two I rotate to [landscape], then switch back to page one: the page is cached in portrait mode, so you end up with the 70/30 split page.
ritchielee
 
Posts: 8
Joined: Wed Sep 28, 2011 3:20 pm

Re: webView and device rotation

Postby cactuscraig » Mon Oct 03, 2011 11:45 pm

Code: Select all
<script type="text/javascript">
    function NKIsPageSupportsAutoOrientation()
    {
        return "yes";
    }
   
   
    var w = new NKWebView();
    w.init(0,20,window.innerWidth,window.innerHeight);
   
    w.loadURL("http://nimblekit.com/");
    w.setScalesToFit("yes");
    w.show();
   
    function reInitWeb()
    {
        NKLog ("WIDTH: " + window.innerWidth);
        w.init(0,20,window.innerWidth,window.innerHeight);
    }
   
    function onPageShown()
    {
        reInitWeb();

    }
    function pageDidRotate(degree) {
        NKLog ("!");
        setTimeout("reInitWeb()",500); 
           
    }
   
   
       
    var navController = new NKNavigationController();
    navController.setTitle("NavCon");
   
    var nkb = new NKButton();
    nkb.init(0,0,100,20,"pageTwo()");
    nkb.setTitle("Page 2");
    nkb.show();
   
     modal = new NKModalWindow();
     var nkt = new NKTextField();
    function pageTwo()
    {
        navController.gotoPage("page2.html");
       
       
           
    }
 
   
    </script>
User avatar
cactuscraig
 
Posts: 846
Joined: Thu Feb 03, 2011 5:15 pm
Location: Scottsdale, AZ

Re: webView and device rotation

Postby ritchielee » Tue Oct 04, 2011 10:43 am

Awesome, but with just one little tweak, as the timeout still caused a jump.

This works perfectly:

Code: Select all
function pageWillRotate(degree) { 
webView.init(0,0,window.innerWidth,window.innerHeight);
}

function pageDidRotate(degree) { 
webView.init(0,0,window.innerWidth,window.innerHeight);
}

function reInitWeb() {
webView.init(0,0,window.innerWidth,window.innerHeight);
}

function onPageShown() {
reInitWeb();
}
ritchielee
 
Posts: 8
Joined: Wed Sep 28, 2011 3:20 pm

PreviousNext

Return to Sample Code

Who is online

Users browsing this forum: No registered users and 2 guests