【发布时间】:2011-08-23 05:24:10
【问题描述】:
我正在开发一个项目,我在 UIscrollview 中添加了 UIWebviews 以显示描述。我这样做是因为我想添加滑动效果以移动到新的描述页面。现在,我想在方向改变(即纵向到横向或反之亦然)时调整 UIscrollview 及其内容(即 uiwebview)的大小。
请给我任何示例代码或任何建议。
【问题讨论】:
标签: iphone uiwebview uiscrollview resize
我正在开发一个项目,我在 UIscrollview 中添加了 UIWebviews 以显示描述。我这样做是因为我想添加滑动效果以移动到新的描述页面。现在,我想在方向改变(即纵向到横向或反之亦然)时调整 UIscrollview 及其内容(即 uiwebview)的大小。
请给我任何示例代码或任何建议。
【问题讨论】:
标签: iphone uiwebview uiscrollview resize
要调整 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));
}
【讨论】:
willAnimateRotationToInterfaceOrientation:中设置您的框架。你还需要什么?
您可以设置scrollView的autoresizingMask,并根据您的要求将此代码添加到您的.m文件中。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * someValue1,
scrollView.frame.size.height * someValue2);
}
【讨论】:
这是一个类似问题及其解决方案的链接: Scale image to fit screen on iPhone rotation
他们对 ScrollView 和 ImageView 使用了 AutoresizingMasks 和 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
}
}
【讨论】: