iOS 8中自定義UIWindows不能正確旋轉
讓您的應用程式進入橫向模式並執行以下程式碼:
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; toastWindow.hidden = NO; toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [toastWindow removeFromSuperview]; });
在iOS 7中,您將獲得透明的藍色疊加層,整個螢幕頂部將在5秒鐘後消失.在iOS 8中,您將獲得一個透明的藍色疊加層,覆蓋了一半以上的螢幕
這顯然與蘋果在iOS 8中的變化有關,螢幕座標現在是面向介面而不是面向裝置,但是在真正的蘋果時尚中,他們似乎在風景模式和旋轉中遺留了無數的錯誤.
我可以通過檢查裝置方向是否是橫向,並翻轉主螢幕邊框的寬度和高度來“修復”,但是當Apple在iOS 9中再次更改所有內容時,這似乎是一個可怕的黑客.
CGRect frame = [[UIScreen mainScreen] bounds]; if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) { frame.size.width = frame.size.height; frame.size.height = [[UIScreen mainScreen] bounds].size.width; } UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:frame]; toastWindow.hidden = NO; toastWindow.backgroundColor = [[UIColor cyanColor] colorWithAlphaComponent:0.5f]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [toastWindow removeFromSuperview]; });
有沒有人遇到這個問題,遇到一個更好,更不脆弱的解決方案?
編輯:我知道我只能使用UIView並將其新增到關鍵視窗,但我想把東西放在狀態列的頂部.
因為另一個原因,你的’修復’並不好,因為它實際上並沒有旋轉視窗,以便文字和其他子檢視以適當的方向出現.換句話說,如果你想通過其他子檢視來增強視窗,那麼它們將被錯誤地導向.
…
在iOS8中,您需要設定視窗的rootViewController,並且該rootViewController需要從“shouldAutoRotate”和“supportedInterfaceOrientations”返回適當的值.還有一些關於這一點:ofollow,noindex" target="_blank">https://devforums.apple.com/message/1050398#1050398
如果您的視窗沒有rootViewController,您可以有效地告訴框架視窗不應該自動旋轉.在iOS7中,這並沒有什麼不同,因為框架並沒有為你做這個工作.在iOS8中,該框架正在處理旋轉,並且它認為它在您限制視窗的邊界時正在執行您所請求的(通過使用nil rootViewController).
嘗試這個:
@interface MyRootViewController : UIViewController @end @implementation MyRootViewController - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAll; } - (BOOL)shouldAutorotate { return YES; } @end
現在,在例項化之後,將rootViewController新增到您的視窗中:
UIWindow *toastWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; toastWindow.rootViewController = [[MyRootViewController alloc]init];
http://stackoverflow.com/questions/26706050/custom-uiwindows-do-not-rotate-correctly-in-ios-8