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