如何在iPhone方向改变时调整uiscrollview及其内容的大小



我正在做一个项目,我在UIscrollview中添加了UIWebviews来显示描述。我这样做是因为我想添加滑动效果来移动到新的描述页面。现在,我想要调整UIscrollview和它的内容(即uiwebview)的大小,当方向改变(即纵向到横向或反之亦然)。

请给我任何示例代码或任何建议。

要调整webview的大小,你必须写:-

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

你可以把你的代码放在下面的代码块中:

- (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 
}

和以下代码将负责方向更改:

- (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));
}

您可以设置scrollView的autoresizingMask,并根据需要将此代码添加到您的.m文件中。

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

这是一个类似问题的链接和他们的解决方案:缩放图像以适应iPhone旋转屏幕

他们对ScrollView和ImageView使用了autoresizingmask和ContentModes的组合,尽管我想同样的解决方案也适用于你的WebView。

每当方向改变

- (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.. 
}
例如

- (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              
}
}

最新更新