【问题标题】:Webview resizes automatically to portrait and landscape view in iphoneWebview 自动调整大小为 iphone 中的纵向和横向视图
【发布时间】:2011-01-20 12:19:22
【问题描述】:

我是 iphone 开发的新手。在我的应用程序中,我想在具有 c 纵向方向的设备中显示 web 视图。它还应该支持横向方向。我使用了以下方法,但没有预期的输出。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{

return (interfaceOrientation == UIInterfaceOrientationPortrait || 

 interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == 

UIInterfaceOrientationLandscapeRight);
}

请指导我。请帮助我。谢谢

【问题讨论】:

    标签: iphone uiwebview landscape screen-orientation


    【解决方案1】:

    我认为您已经为 webview 设置了固定框架。这在实现方向更改时无济于事

    试试这个:

    使用此代码显示网络视图。使用您的 URL 更改堆栈溢出 URL;

    contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    self.view = contentView;
    self.view.autoresizesSubviews = YES;
    
    
    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
    webFrame.origin.y -= 20.0;
    
    webView = [[UIWebView alloc] initWithFrame:webFrame];
    
    [contentView addSubview:webView]; 
    webView.scalesPageToFit = YES;
    webView.autoresizesSubviews = YES;
    webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    [webView setBackgroundColor:[UIColor clearColor]];
    NSString *urlAddress = @"https://www.stackoverflow.com";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];
    webView.delegate =self;
    [webView release];
    

    支持方向

       - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation 
    {
    return YES;
    
    }
    

    网页视图随方向变化

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
    if(fromInterfaceOrientation == UIInterfaceOrientationPortrait){
     [webView stringByEvaluatingJavaScriptFromString:@"rotate(0)"];
    
    }
    else{
        [webView stringByEvaluatingJavaScriptFromString:@"rotate(1)"];
    }
    }
    

    希望以上内容能满足您的要求。 一切顺利。

    【讨论】:

    • 谢谢。我想在我的 web 视图中显示工具栏?我怎样才能做到这一点?我无法在我的 web 视图中看到工具栏。
    • @pugal 插入标签栏作为子视图 ... [self.view insertSubview:tBar belowSubview:contentView];将 tBar 更改为您使用的工具栏。
    【解决方案2】:

    如果您想支持任何方向,只需返回YES

    - (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation 
    {
        return YES;
    }
    

    【讨论】:

    猜你喜欢
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2014-10-22
    • 2011-09-05
    • 1970-01-01
    • 2014-11-28
    相关资源
    最近更新 更多