我已经在视图控制器的视图上添加了一个webview,一个titleLabel和一个coverflowView作为它的子视图,我想让它在方向改变时改变大小。我用这个方法改变了webview的框架:
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration
如果我以纵向启动应用程序,当我将iPad从orientationLandscape旋转到orientationPortrait或从orientationPortrait旋转到orientationLandscape时,它的内容确实调整了大小,但奇怪的是,当我使用横向orientation...But启动应用程序时,它的内容没有调整大小。NSLog显示框架已经改变。至于titleLabel和coverflowView,它们可以正确地调整大小。我怀疑是不是因为css ?我用css根据webview.Does的高度和宽度来控制内容的样式,有谁能帮我找出原因吗?代码如下:
- (void)willRotateToInterfaceOrientation: (UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration {
double i = 0;
NSInteger width=self.view.frame.size.width;
NSInteger height=self.view.frame.size.height;
NSLog(@"view :%@",[self.view description]);
switch (toInterfaceOrientation){
case UIInterfaceOrientationPortrait:
{
NSLog(@"rotate to Portrait");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
self.docView.frame=CGRectMake(0, 50, width+20, height-70);
self.toolbar.frame=CGRectMake(0, 0,height , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag){
view.frame=CGRectMake(width-60, 6, 50, 36);
}else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(width-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , width+20, height/2-50)];
[titleLabel setFrame:CGRectMake(width/2-40,height/2-100, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 768, 1004);
}else{
self.viewer.frame=CGRectMake(0, 0, 320, 480);
}
}
i=0;
}break;
case UIInterfaceOrientationPortraitUpsideDown:
{
NSLog(@"rotate to PortraitUpsideDown");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
self.docView.frame=CGRectMake(0, 50, width+20, height-70);
self.toolbar.frame=CGRectMake(0, 0,height , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag) {
view.frame=CGRectMake(width-60, 6, 50, 36);
}else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(width-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , width+20, height/2-50)];
[titleLabel setFrame:CGRectMake(width/2-40,height/2-100, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 768, 1004);
}else{
self.viewer.frame=CGRectMake(0, 0, 320, 480);
}
}
i=180;
} break;
case UIInterfaceOrientationLandscapeLeft:{
NSLog(@"rotate to LandscapeLeft");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// self.coverflow.frame=CGRectMake(0, 0, height+20, width-20);
self.docView.frame=CGRectMake(0, 50, height+20, width-70);
self.toolbar.frame=CGRectMake(0, 0,height+20 , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag) {
view.frame=CGRectMake(height-60, 6, 50, 36);
NSLog(@"button %@",[view description]);
}else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(height-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , height+20, width/2-50)];
[titleLabel setFrame:CGRectMake(height/2-40,width/2-80, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 1024, 748);
}else{
self.viewer.frame=CGRectMake(0, 0, 480, 320);
}
}
i = 90;
}break;
case UIInterfaceOrientationLandscapeRight:{
NSLog(@"rotate to LandscapeRight");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
// self.coverflow.frame=CGRectMake(0, 0, height+20, width-20);
self.docView.frame=CGRectMake(0, 50, height+20, width-70);
self.toolbar.frame=CGRectMake(0, 0,height+20 , 50);
for (UIView * view in [toolbar subviews]) {
if ([view isKindOfClass:[UIButton class]] && view.tag==kBackButtonTag) {
view.frame=CGRectMake(height-60, 6, 50, 36);
}
else if([view isKindOfClass:[UIButton class]] && view.tag==kReloadButtonTag){
view.frame=CGRectMake(height-160, 6, 80,36 );
}
}
[coverflow setFrame:CGRectMake(0, 0 , height+20, width/2-50)];
[titleLabel setFrame:CGRectMake(height/2-40,width/2-80, 100, 20)];
if ([[[UIDevice currentDevice]model]isEqualToString:@"iPad"]) {
self.viewer.frame=CGRectMake(0, 0, 1024, 748);
}else{
self.viewer.frame=CGRectMake(0, 0, 480, 320);
}
}
i = -90;
}break;
}
//[webViewController willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
// [self.view setNeedsDisplay];
// NSLog(@"coverflowView :%@",[self.coverflow description]);
NSLog(@"webview :%@",[viewer description]);
[viewer stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.__defineGetter__('orientation',function(){return %f;});",i]];
[viewer stringByEvaluatingJavaScriptFromString:@"var e = document.createEvent('Events'); e.initEvent('orientationchange', true, false); document.dispatchEvent(e); "];
}
- (void)viewDidLoad {
self.view.clipsToBounds=YES;
self.view.autoresizesSubviews=YES;
// self.view.autoresizingMask=UIViewAutoresizingNone;
viewer=[[UIWebView alloc]initWithFrame:self.view.bounds];
[self.view addSubview:viewer];
viewer.delegate=self;
viewer.scalesPageToFit=NO;
viewer.autoresizesSubviews=NO;
viewer.autoresizingMask=UIViewAutoresizingNone;
viewer.dataDetectorTypes=0;
// viewer.autoresizingMask=UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
NSLog(@"webView :%@",[viewer description]);
// [viewer setFrame:CGRectMake(0, self.view.bounds.size.height/2 , self.view.bounds.size.width, self.view.bounds.size.height/2)];
// [viewer setBounds:CGRectMake(0, self.view.bounds.size.height/2 , self.view.bounds.size.width, self.view.bounds.size.height/2)];
UIScrollView *scroller=[viewer.subviews objectAtIndex: 0];
if (scroller) {
scroller.alwaysBounceVertical=NO;
scroller.bounces=NO;
scroller.scrollEnabled=NO;
}
[self viewHomePage];
//[self createCoverFlowView];
[self createPopView];
//[self setHomeButtonPosition];
//[self setSettingButtonPosition];
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30000
UILongPressGestureRecognizer* longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
[self.view addGestureRecognizer:longPress];
longPress.minimumPressDuration=2.0;
longPress.delegate = self;
longPress.cancelsTouchesInView = NO;
longPress.allowableMovement=20;
[longPress release];
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[self.view addGestureRecognizer:singleTap];
singleTap.delegate = self;
singleTap.cancelsTouchesInView = NO;
[singleTap release];
#endif
//[viewer setOpaque:YES]; //透明
[super viewDidLoad];
}
发布于 2011-05-24 08:25:29
我花了大约两天的时间试图追踪这个问题,因为我遇到了完全相同的问题。我发现,如果我的视图开始于纵向模式,它将正确地调整到横向。但是,如果我在横向模式下开始视图,它将保持相同的大小在肖像。
如果你的视图是整个设备的宽度,只需添加下面的meta标签即可。
<meta name="viewport" content="width=device-width" />
但是,在我的例子中,我们的UIWebView是一个非标准大小。将视口设置为device-width
只会使情况变得更糟!通过使用javascript动态设置视区,我能够正确地完成所有工作。这应该在UIWebViews父视图的layoutSubviews期间完成。你可以在willRotateToInterfaceOrientation中这样做,但我发现如果你在willRotate中这样做,一些大小和接口可能无法正确设置。先在那里试一下,看看动画是如何工作的。
代码只是将视口大小设置为当前帧大小。您正在查看的超文本标记语言必须已经有一个meta viewport
标记(如上所述)。如果您不能编辑HTML,还有其他方法可以通过javascript添加它。最简单的方法是将meta标记添加到<head>
标记中。然后,在正确设置帧之后,使用以下代码:
// assuming your self.viewer is a UIWebView
[self.viewer stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:
@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ",
(int)self.viewer.frame.size.width]];
如果不希望用户缩放或要设置最大缩放比例,可以添加其他视口项目。这最终使得视图对于我们的UIWebView来说是正确的。
发布于 2011-05-16 01:31:26
我也有类似的问题,但仍然找不到真正的解决方案。我在UIWebView上调用reload来解决这个问题,方法是让scales页面适合我。
[webView reload];
发布于 2011-11-09 19:42:42
如果其他人也有同样的问题,我只是给他们一个建议。
webview无法正确调整大小,因为HTML已经呈现(我不确定这是否完全正确,可能只是webview本身的一个bug ),但不管怎样。我创建了一个调整webview html正文宽度的方法:
- (void) resizeHtml
{
NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('html')[0].style.width= '%dpx'", (int) _frame.size.width];
[self.webView stringByEvaluatingJavaScriptFromString:jsString];
[jsString release];
}
这在iphone上似乎工作得很好,但在ipad上我不得不这样做
- (void) reloadHtml
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];
if(html == nil || [html isEqualToString:@""] || [html length] < 100)
[self.webView reload];
[self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"some base url"]];
}
您也可以使用self.webview重新加载,但这会向服务器发出另一个请求,我认为这是不好的;-)
我希望这能帮到你!
https://stackoverflow.com/questions/6007904
复制相似问题