-1

如果我啓動我的應用程序,我想知道設備的方向。 在我的主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://stackoverflow.com/a/24398249/5629933)解決了我的問題。我是Objective-C的新手,但確實有意義,儘管我在創建時將我的UIWebView的大小設置爲與我的UIView相同?

+1

你正在做的一些事情太早了,這就是爲什麼你得到無用的結果。但是你根本不需要知道設備的方向。使用自動佈局來定位您的網頁視圖。這樣,無論發生什麼事,它都會處於正確的位置。 – matt

+0

啊好吧我明白,但大小設置不正確。如果您可以在我的方法中看到我創建「網頁視圖」,則其大小始終爲320寬度和568高度。想象一下,如果這個視圖的方向設置爲橫向,我的「網頁視圖」方向正確,但尺寸不適合整個屏幕。同樣的問題,如果我的設備處於橫向模式只有我的網站的大小不適合整個屏幕,因爲我的'UIView'的寬度總是320寬度和568高度@matt。我如何解決它? – Premox

+0

該截圖沒有意義。什麼是'self.view'?什麼部分應該是網絡視圖?網頁加載到網頁視圖的哪一部分? – rmaddy

回答

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]; 
} 
+0

我也這麼認爲,但如果網站配置爲風景,當我開始我的應用程序它不適合我的整個屏幕。它只有320寬和568高的尺寸。該網站有正確的方向,但我的'網絡視圖'的大小不正確。它只有景觀的一半大小@rmaddy – Premox

+0

我很困惑。你的問題是網絡視圖的大小不是填充'self.view'還是加載的網站沒有正確填充網頁視圖的問題? – rmaddy

+0

我的問題是,我的'網絡視圖'不填寫我的'self.view' – Premox

1

您的整個createWebView方法是不明智的。你根本不應該關心方向。只需將您的網絡視圖添加到self.view並使用自動佈局進行定位。這樣,當佈局完成後,它將處於正確的位置/大小,而不管方向如何或self.view可能發生的其他任何事情。

相關問題