如果我啓動我的應用程序,我想知道設備的方向。 在我的主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代表橫向)。 webView
是UIWebView
的類型。
因此,如果我以縱向方式啓動應用程序和設備,我想創建320寬度和568高度的webView
。如果它在橫向啓動,我想創建寬度爲568和高度爲320的webView
。
我的問題是在啓動時檢測到我的設備的方向。我還搜查,發現了一些類似的線程(Get launch orientation of iPad app或iOS: 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
相同?
你正在做的一些事情太早了,這就是爲什麼你得到無用的結果。但是你根本不需要知道設備的方向。使用自動佈局來定位您的網頁視圖。這樣,無論發生什麼事,它都會處於正確的位置。 – matt
啊好吧我明白,但大小設置不正確。如果您可以在我的方法中看到我創建「網頁視圖」,則其大小始終爲320寬度和568高度。想象一下,如果這個視圖的方向設置爲橫向,我的「網頁視圖」方向正確,但尺寸不適合整個屏幕。同樣的問題,如果我的設備處於橫向模式只有我的網站的大小不適合整個屏幕,因爲我的'UIView'的寬度總是320寬度和568高度@matt。我如何解決它? – Premox
該截圖沒有意義。什麼是'self.view'?什麼部分應該是網絡視圖?網頁加載到網頁視圖的哪一部分? – rmaddy