[英]Device orientation handling in iOS 6 during view load?
我有一個Tab欄應用程序。 我正在使用XCode 4.3.3。 我用iOS6的東西升級到了4.5.2。
我在每個視圖的shouldAutorotateToInterfaceOrientation
代碼將檢查當前的設備方向並正確放置所有UI組件。
但升級到iOS 6后,此代碼未執行。 我已經嘗試了差不多一天來解決這個問題,但沒有運氣。
我也試過了
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
在viewLoad
, viewDidLoad
, viewWillAppear
如果我想在視圖加載期間檢查方向,並在視圖加載期間將UI組件放置到適當的位置,我該怎么辦? 請提出你的建議。
謝謝
如果你想檢查哪個是當前方向,那么你只需要寫下prifix.pch下面的行
#define isPortrait [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown
然后你要檢查方向,寫下如下條件
if(isPortrait)
{
//portrait mode....
}
else
{
//landscape mode....
}
讓我知道它的工作與否......
快樂的編碼!
要檢查ios6中的方向,您必須編寫以下方法,並在info.plist中,您必須定義您想要的方向...
讓我知道它是否有效!!!
快樂的編碼!!!!
- (BOOL)shouldAutorotate{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;//you can write here which ever orientation you want..
}
你像這樣管理Oriantation: -
-(BOOL)shouldAutorotate
{
return YES;
}
並在ViewWillApear
設備中每次檢查Oriantation,如: -
- (void)willRotateToOrientation:(UIInterfaceOrientation)newOrientation {
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation))
{
if (newOrientation == UIInterfaceOrientationLandscapeLeft || newOrientation == UIInterfaceOrientationLandscapeRight) {
//set your landscap View Frame
[self supportedInterfaceOrientations];
}
}
else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation))
{
if(newOrientation == UIInterfaceOrientationPortrait || newOrientation == UIInterfaceOrientationPortraitUpsideDown){
//set your Potrait View Frame
[self supportedInterfaceOrientations];
}
}
// Handle rotation
}
-(void)viewWillAppear:(BOOL)animated
{
[self willRotateToOrientation:[[UIDevice currentDevice] orientation]];
[super viewWillAppear:YES];
}
UPDATE
可能人們使用檢查設備orientation
如下所示將此行放入ViewWillApear
: -
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
和
-(void)deviceRotated:(NSNotification*)notification
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//Do your stuff for landscap
}
else if(orientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
//Do your stuff for potrait
}
}
在viewDidLoad方法中,您需要檢查相同的內容,因此您需要編寫代碼,除非它可能影響視圖(如果它不在縱向上)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.