IOS 中禁止横屏

来源:互联网 发布:百度竞价推广优化 编辑:程序博客网 时间:2024/05/22 00:21
#define UIInterfaceOrientationIsPortrait(orientation)  ((orientation) == UIInterfaceOrientationPortrait || (orientation) == UIInterfaceOrientationPortraitUpsideDown)
#define UIInterfaceOrientationIsLandscape(orientation) ((orientation) == UIInterfaceOrientationLandscapeLeft || (orientation) == UIInterfaceOrientationLandscapeRight)


-(NSUInteger)supportedInterfaceOrientations{    return UIInterfaceOrientationMaskPortrait;}


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{     return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);}

其实只需要

//如果是ios6.0以上的:- (BOOL)shouldAutorotate{return NO;}6.0以下的- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return NO;}//NO就是不支持翻转,记住要加在根视图上。具体什么时候返回yes就你自己判断了


对于pad横屏,可以这样使用

-(BOOL)shouldAutorotate{    return YES;}-(NSUInteger)supportedInterfaceOrientations{    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)    {        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;    }        return UIInterfaceOrientationMaskPortrait;}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)    {        return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);    }        return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);}

对于支持同时支持phone和pad,在禁止phone横屏时,还要考虑pad可以横屏,所以要区分。

-(BOOL)shouldAutorotate{    return YES;}//phone只支持正方向,pad只支持左右横屏-(NSUInteger)supportedInterfaceOrientations{    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)    {        return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;    }        return UIInterfaceOrientationMaskPortrait;}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)    {        return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);    }        return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);}


四个朝向:

Portrait: 正方向,下面有home键

Upside Down: 反方向

Landscape Left: 向左横屏,Home键左方

Landscape Right:向右横屏

                                             
0 0
原创粉丝点击