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

时间:2016-01-21 16:45:50

标签: ios objective-c uiwebview uiinterfaceorientation device-orientation

如果我启动了我的应用,我想知道我的设备的方向。 在我的主view controller我有一个UIWebView,我以编程方式添加到主view controller

我用(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检查应用程序是否已启动。 orientNr是一个将应用程序旋转到我想要的给定方向的数字,对解决方案来说并不重要(1是设备,2是横向)。 webView的类型为UIWebView

所以,如果我以纵向启动应用程序和设备,我想创建宽度为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://stackoverflow.com/a/24398249/5629933)解决了我的问题。我是Objective-C的新手,但是当我创建它时,虽然我将UIWebView的大小设置为与UIView相同,但确实有意义吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

不需要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];
}