了解更多,請關注我的微信公眾號:mellong
所有frame的高度和寬度應該通過superview的bounds計算。
xib中的view無法設置auto mask的必須通過代碼設,不設定的話有時可以自動適應,但是有時會出現有部分黑屏的情況。
兩邊都不設置mask則為居中顯示。
以下兩方法為rotate是自動調用,如果該viewController沒有navigationController時,以下兩方法可能不被調用,需要自己加入通知中心。
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
調用此方法時superview.bounds已經改變。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
調用此方法時superview.bounds未改變
獲取當前屏幕方向
UIInterfaceOrientation currentOrient = [UIApplication sharedApplication].statusBarOrientation;
判斷當前設備是否為4寸屏
#define isIPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
3.5與4寸屏高度相差88.f,寬度一樣為320.f
自適應橫屏一般修改automask的autowidth,導航欄和一般控件主要變化的是寬度,高度也變化的一般是可以tableView和scrollView等。
有時候橫屏沒有正確自適應一般是superview.bounds未改變,設置subview frame的時機不對。
- 如果想讓某一個ViewController固定某個方向不旋轉,方法如下:
- 修改AppDelegate.m,加入下列代碼,其中_enablePortrait為新增的變量,用于判斷是否要進行旋轉。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(_enablePortrait)
{
return UIInterfaceOrientationMaskPortrait;
}
return UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait;
}
- 在不需要旋轉的viewController中的下列方法中加入以下代碼即可。
-(void)viewWillAppear:(BOOL)animated
{
AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;
delegate.enablePortrait = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
AppDelegate *delegate = (AppDelegate *)[UIApplicationsharedApplication].delegate;
delegate.enablePortrait = NO;
}
由于使用pushViewController會導致所進入的視圖會根據前一視圖的方向顯示,所以需要用以下方法hack一下,才能使其自動根據設定的方向旋轉。
- (void)updateOrientation
{
[[UIApplicationsharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitanimated:NO];
UIViewController *viewController = [[UIViewControlleralloc] init];
[self presentModalViewController:viewController animated:NO];
[self dismissModalViewControllerAnimated:NO];
[viewController release];
}
- iOS6旋轉發生當時屏幕不旋轉的原因可能是:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
self.window.rootViewController = gameNavController;
}else {
[self.window addSubview:gameNavController.view];
}
在應用中有時需要制定某些頁面是Portrait或者landscape,這時需要在info.plist文件加入對這些方向的支持。
如果window的rootViewController是NavigationController則需繼承該類寫入:
//iOS6以下版本
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}
//iOS6及以上版本
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
由此則全局默認情況下只支持landscape。
注意:navigationController在其子類中指定,在push進去的viewController指定則是無效。
有效的情況為使用presentModalViewController或者其他形式的present,在present的viewController中重寫這三個方法,可以限制其當前的方向只為portrait.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return UIInterfaceOrientationPortrait == toInterfaceOrientation;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}