在启动应用程序时无法检测设备的方向以创建UIWebView的大小正确

问题描述:

如果我启动我的应用程序,我想知道设备的方向。 在我的主view controller我有一个UIWebView,我编程添加到主view controller在启动应用程序时无法检测设备的方向以创建UIWebView的大小正确

我用(int)[[UIApplication sharedApplication] statusBarOrientation]对它进行了测试,但是我只能得到数字1.无论我的设备是纵向还是横向。

我想设置我的尺寸UIWebView但如果我不知道方向如何设置它的大小?

2016-01-21 17:21:45.387 iPhoneApp[1026:151720] 0) Orientation: 1 
2016-01-21 17:21:45.388 iPhoneApp[1026:151720] 0) Orientation: 0 
2016-01-21 17:21:45.393 iPhoneApp[1026:151720] 1) Orientation: 1 
2016-01-21 17:21:45.393 iPhoneApp[1026:151720] 1) Orientation: 0 
2016-01-21 17:21:45.406 iPhoneApp[1026:151720] 2) Orientation: 1 
2016-01-21 17:21:45.406 iPhoneApp[1026:151720] 2) Orientation: 1 
2016-01-21 17:21:45.417 iPhoneApp[1026:151720] 3) Orientation: 1 
2016-01-21 17:21:45.417 iPhoneApp[1026:151720] 3) Orientation: 1 

0是didFinishLaunchingWithOptions

1是viewDidLoad

2是viewDidAppear

3是我的方法,我创造我UIWebView

NSLog(@"3) Orientation: %d", (int)[[UIApplication sharedApplication] statusBarOrientation]); 
NSLog(@"3) Orientation: %d", (int)[[UIDevice currentDevice] orientation]); 

这总是我的日志的顺序。

,我创建的方法我UIWebView看起来像

-(void)createWebView { 
    //NSLog(@"createWebView"); 
    //NSLog(@"orientation: %d", [[self orientNr] intValue]); 
    [self destroyWebView]; 
    NSLog(@"3) Orientation: %d", (int)[[UIApplication sharedApplication] statusBarOrientation]); 
    NSLog(@"3) Orientation: %d", (int)[[UIDevice currentDevice] orientation]); 
    if(([self firstLoad] && [[self orientNr] isEqualToNumber:[[NSNumber alloc] initWithInt:2]]) || 
     ([self firstLoad] && [[self orientNr] isEqualToNumber:[[NSNumber alloc] initWithInt:1]] && UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]))) 
     [self setWebView:[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)]]; 
    else 
     [self setWebView:[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]]; 
    dispatch_async(dispatch_get_main_queue(), ^{ 
     [[self view] addSubview:[self webView]]; 
    }); 
    [[self webView] setScalesPageToFit:YES]; 
    [[self webView] setDelegate:self]; 
    [[self webView] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; 
    [[self view] setAutoresizesSubviews:YES]; 
    //NSLog(@"iPhoneApp: %@", [self webView]); 
} 

变量firstLoad检查如果应用程序尚未启动。​​是一个将应用程序旋转到我想要的给定方向的数字,对解决方案不重要(1代表设备,2代表横向)。 webViewUIWebView的类型。

因此,如果我以纵向方式启动应用程序和设备,我想创建320宽度和568高度的webView。如果它在横向启动,我想创建宽度为568和高度为320的webView

我的问题是在启动时检测到我的设备的方向。我还搜查,发现了一些类似的线程(Get launch orientation of iPad appiOS: Device orientation on load),但它并没有解决我的问题

编辑:

,如果我有我的设备上的风景或UIWebView应在景观在推出开始时才会发生应用程序

编辑2:

问题解决。我只加

-(void)viewDidLayoutSubviews { 
    [super viewDidLayoutSubviews]; 
    [[self webView] setFrame:[[self view] bounds]]; 
} 

和我UIWebView始终像我UIView大小相同。也在横向方向。 这里的答案(https://*.com/a/24398249/5629933)解决了我的问题。我是Objective-C的新手,但确实有意义,尽管我在创建时将我的UIWebView的大小设置为与我的UIView相同?

+1

你正在做的一些事情太早了,这就是为什么你得到无用的结果。但是你根本不需要知道设备的方向。使用自动布局来定位您的网页视图。这样,无论发生什么事,它都会处于正确的位置。 – matt

+0

啊好吧我明白,但大小设置不正确。如果您可以在我的方法中看到我创建“网页视图”,则其大小始终为320宽度和568高度。想象一下,如果这个视图的方向设置为横向,我的“网页视图”方向正确,但尺寸不适合整个屏幕。同样的问题,如果我的设备处于横向模式只有我的网站的大小不适合整个屏幕,因为我的'UIView'的宽度总是320宽度和568高度@matt。我如何解决它? – Premox

+0

该截图没有意义。什么是'self.view'?什么部分应该是网络视图?网页加载到网页视图的哪一部分? – rmaddy

您的createWebView方法中的大部分代码都是不需要的。最重要的是在添加Web视图之前设置autoresizingMask

-(void)createWebView { 
    //NSLog(@"createWebView"); 
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 
    self.webView.scalesPageToFit = YES; 
    self.webView.delegate = self; 
    self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [self.view addSubview:self.webView]; 
} 
+0

我也这么认为,但如果网站配置为风景,当我开始我的应用程序它不适合我的整个屏幕。它只有320宽和568高的尺寸。该网站有正确的方向,但我的'网络视图'的大小不正确。它只有景观的一半大小@rmaddy – Premox

+0

我很困惑。你的问题是网络视图的大小不是填充'self.view'还是加载的网站没有正确填充网页视图的问题? – rmaddy

+0

我的问题是,我的'网络视图'不填写我的'self.view' – Premox

您的整个createWebView方法是不明智的。你根本不应该关心方向。只需将您的网络视图添加到self.view并使用自动布局进行定位。这样,当布局完成后,它将处于正确的位置/大小,而不管方向如何或self.view可能发生的其他任何事情。