2011-08-23 22 views
1

我正在一個項目中,我已經在UIscrollview中添加UIWebviews來顯示說明。我這樣做是因爲我想添加滑動效果來移動到新的描述頁面。現在,我想調整UIscrollview及其內容(即uiwebview),當方向改變時(即縱向到橫向或Viceversa)。如何調整uiscrollview及其內容在iPhone中的方向變化?

請給我任何示例代碼或任何建議。

回答

0

你可以把你的代碼下面的代碼塊:

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

} 
+0

感謝它。我能夠檢測方向。但問題是調整UIscrollview及其內容。 Plz在那裏幫助我。 –

+0

你可以在'willAnimateRotationToInterfaceOrientation:'中設置你的框架。你還需要什麼 ? – Devang

+0

我只與我滾動視圖,我想改變其內容的大小。怎麼做? –

0

您可以設置滾動視圖的autoresizingMask並添加此代碼根據您的要求將其添加到.m文件中。

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

它是否會調整uiwebview的大小,這是uiscrollview的內容 –

+0

如果您在xib文件中製作視圖的速度比通過旋轉視圖來測試視圖要好,否則您需要創建一個轉儲xib文件來檢查您需要的autoresizingMask應用 – Robin

1

當過方向改變

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

} 

} 
0

下面是一個類似的問題,他們的解決方案的鏈接: Scale image to fit screen on iPhone rotation

他們使用的組合S的AutoresizingMasks和ContentModes crollView,在他們的情況下,一個ImageView,但我想象相同的解決方案將適用於您的WebView。

1

要調整,你必須編寫web視圖: -

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

準確地說,我是這個領域的新手,但我想知道如果你不使用void,那麼在它將返回值爲主要@shweta – SagarPPanchal

+0

好作品,@shweta :) – SagarPPanchal