【问题标题】:IOS UIWebView Orientation issueIOS UIWebView 方向问题
【发布时间】:2014-01-17 06:34:49
【问题描述】:

我是 iOS 新手,只需要制作一个加载 URL 的小型原生应用程序,该应用程序托管在 IIS 上并在 Jquery 移动设备上制作。 问题是当我改变方向时,它从右侧剪裁,而同样的东西在所有 android 设备上都正常工作。 请帮帮我

这是我的代码

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(0.0, 0.0,768,1024)];


    NSString *httpSource=@"http://www.mydomain.com";
    NSURL *fullUrl=[NSURL URLWithString:httpSource];
    NSURLRequest *httpRequest=[NSURLRequest requestWithURL:fullUrl];
    webView.delegate=self;
    [webView loadRequest:httpRequest];
    [self.view addSubview:webView];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

【问题讨论】:

    标签: objective-c cocoa-touch ios6 uiwebview


    【解决方案1】:

    你必须为你的 webView 设置 Autoresizing mask,或者你可以使用 Autolayout。

    - (void)viewDidLoad {
    
           [super viewDidLoad];
    
           //Here you use the frame of Total View, irrespective of device type
           UIWebView *webView=[[UIWebView alloc]initWithFrame:self.view.bounds];
    
           //Autoresizemask
           webView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
           NSString *httpSource=@"http://www.mydomain.com";
           NSURL *fullUrl=[NSURL URLWithString:httpSource];
           NSURLRequest *httpRequest=[NSURLRequest requestWithURL:fullUrl];
           webView.delegate=self;
           [webView loadRequest:httpRequest];
           [self.view addSubview:webView];
         }
    

    【讨论】:

    • 感谢 chandu 您的代码有效。但是如果我想让这个应用程序在 iphone 和 ipad 上运行呢?宽度和高度应该是多少
    • 您可以使用 View 的总帧作为您的 webView 的帧,这样您就不必手动设置帧,无论是哪种设备。我建议你通读苹果文档。检查我更新的代码。
    【解决方案2】:

    尝试在设备旋转时更改您的UIWebView 框架。试试下面的代码

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
        [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
        [webView setFrame: CGRectMake(0, 0, 1024, 768)]; 
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-01
      • 1970-01-01
      • 2012-12-30
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 2013-05-24
      相关资源
      最近更新 更多