在iOS5.1 和 之前的版本中, 我們通常利用 shouldAutorotateToInterfaceOrientation: 來單獨(dú)控制某個(gè)UIViewController的旋屏方向支持,比如:
復(fù)制代碼 代碼如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
但是在iOS6中,這個(gè)方法被廢棄了,使用無效。
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. (Deprecated in iOS 6.0. Override the supportedInterfaceOrientations andpreferredInterfaceOrientationForPresentation methods instead.)
實(shí)踐后會(huì)發(fā)現(xiàn),通過supportedInterfaceOrientations的單獨(dú)控制是無法鎖定屏幕的。
復(fù)制代碼 代碼如下:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
多次實(shí)驗(yàn)后總結(jié)出控制屏幕旋轉(zhuǎn)支持方向的方法如下:
子類化UINavigationController,增加方法
復(fù)制代碼 代碼如下:
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
并且設(shè)定其為程序入口,或指定為 self.window.rootViewController
隨后添加自己的view controller,如果想禁止某個(gè)view controller的旋屏:(支持全部版本的控制)
復(fù)制代碼 代碼如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
如果想又開啟某個(gè)view controller的全部方向旋屏支持:
復(fù)制代碼 代碼如下:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
從而實(shí)現(xiàn)了對(duì)每個(gè)view controller的單獨(dú)控制。
順便提一下,如果整個(gè)應(yīng)用所有view controller都不支持旋屏,那么干脆:
復(fù)制代碼 代碼如下:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
橫豎屏切換,視圖亂了怎么辦?
首先,我們必須了解一下下列4種狀態(tài),它們被用來描述設(shè)備旋轉(zhuǎn)方向:

對(duì)于旋屏的處理,大致分為如下幾種情況和思路:
也許,你不需要旋屏支持,而希望鎖定屏幕
復(fù)制代碼 代碼如下:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
也許,你需要支持旋屏,或者支持部分方向的旋屏
復(fù)制代碼 代碼如下:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
也許,你的view有張背景圖,旋屏?xí)r系統(tǒng)幫助你拉伸了圖片,但是卻沒有管你的其它部件,比如button,你希望直接改變button的大小和位置
復(fù)制代碼 代碼如下:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
NSLog(@"現(xiàn)在是豎屏");
[btn setFrame:CGRectMake(213, 442, 340, 46)];
}
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
NSLog(@"現(xiàn)在是橫屏");
[btn setFrame:CGRectMake(280, 322, 460, 35)];
}
}
也許,你并不希望用絕對(duì)坐標(biāo)去約束控件,而是希望讓它通過旋轉(zhuǎn)自己適應(yīng)屏幕的旋轉(zhuǎn)
復(fù)制代碼 代碼如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
//利用 NSNotificationCenter 獲得旋轉(zhuǎn)信號(hào) UIDeviceOrientationDidChangeNotification
NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];
[ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(void)rotation_btn:(float)n
{
UIButton *robtn = self.btn;
robtn.transform = CGAffineTransformMakeRotation(n*M_PI/180.0);
}
-(void)orientationChanged
{
UIDeviceOrientation orientaiton = [[UIDevice currentDevice] orientation];
switch (orientaiton) {
caseUIDeviceOrientationPortrait:
[self rotation_btn:0.0];
break;
caseUIDeviceOrientationPortraitUpsideDown:
[self rotation_btn:90.0*2];
break;
caseUIDeviceOrientationLandscapeLeft:
[self rotation_btn:90.0*3];
break;
caseUIDeviceOrientationLandscapeRight:
[self rotation_btn:90.0];
break;
default:
break;
}
}
也許,你需要autoresizesSubviews = YES
也許,你希望橫豎屏有不同的布局效果,需要準(zhǔn)備2份Subview,在不同狀態(tài)去替換
當(dāng)然不要忘記,需要調(diào)節(jié)設(shè)定圖示中的1、2處,

來幫助我們完成自己想要的適應(yīng)效果。Example 動(dòng)畫呈現(xiàn)的很清晰,^_^ 我就不再
主站蜘蛛池模板:
方城县|
普定县|
阿荣旗|
成武县|
新野县|
剑川县|
施秉县|
原平市|
商城县|
天全县|
灵山县|
海南省|
浦县|
乌审旗|
富裕县|
上杭县|
惠州市|
通州市|
隆林|
弋阳县|
昌图县|
永善县|
松江区|
富民县|
伊金霍洛旗|
金平|
泗洪县|
宜都市|
遂宁市|
开阳县|
遂溪县|
大邑县|
西昌市|
繁昌县|
淮安市|
上思县|
施秉县|
瓮安县|
武定县|
临澧县|
共和县|