开发者

How to resize uiscrollview and its content on the change of orientation in iPhone?

开发者 https://www.devze.com 2023-03-30 04:06 出处:网络
I am working on a project where I have added UIWebviews in the UIscrollview to show the description. I did this because I want to add the swipe effect to move to the new description page. Now, I want

I am working on a project where I have added UIWebviews in the UIscrollview to show the description. I did this because I want to add the swipe effect to move to the new description page. Now, I want to resize that UIscrollview and its content (i.e uiwebview) when the orientation changes (i.e portrait to landscape or Viceversa).

Please give me any sample 开发者_开发技巧code or any suggestions for it.


To resize the webview you have to write:-

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    webview.scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1, 
                                        scrollView.frame.size.height * someValue2);
}


You can put your code in the following code block :

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if ([self isPadPortrait]) {
    // Your code for Portrait
    // set frame here  
} else if ([self isPadLandscape]) {
   // Your code for Landscape
   // set frame here 
}

and following code will take care of orientation change :

- (BOOL)isPadPortrait
{

    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationPortrait
                || self.interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown));
}

- (BOOL)isPadLandscape
{

    return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad
            && (self.interfaceOrientation == UIInterfaceOrientationLandscapeRight
                || self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft));

}


You can set the autoresizingMask of scrollView and add this code to your .m file according to your requirement.

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1, 
                                        scrollView.frame.size.height * someValue2);
}


Here is a link to a similar question and their solution: Scale image to fit screen on iPhone rotation

They used a combination of AutoresizingMasks and ContentModes for both the ScrollView and, in their case, an ImageView, although I imagine the same solution would work for your WebView.


when ever the orientation changed

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

{

// this delegate method will called if we set should auto orientation return yes;
when ever we changed orientation  so set frames.. 

}

for example

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

{
    appDelegate.interface=interfaceOrientation;


    if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)

{

//  SET FRAMES FOR LANDSCAPE HERE       


}

    if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationPortrait)

{ 
        //SET FRAMES FOR Portrait HERE              

}

}
0

精彩评论

暂无评论...
验证码 换一张
取 消