iphone处理翻转[landscape]

本文介绍在iOS开发中处理屏幕旋转的三种方案,包括无UITabBar时的支持旋转方法,有UITabBar时的实现方式,以及涉及UITabBar隐藏与显示的复杂场景处理。

方案1.

当没有UITabbar 时候 只需要重载两个方法即可,第一个方法是支持翻转,第二个方法调整根据不同的翻转模式,调整界面元素位置/或新做

一个view视图,为横向时候把横向视图添加到当前视图里面,翻转到纵向模式时候,把视图从当前视图移出[removefromsuperview]

 

 

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   //return YES;

   return ((interfaceOrientation == UIInterfaceOrientationPortrait)

             ||(interfaceOrientation == UIInterfaceOrientationLandscapeLeft)

    ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight));

}

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

方案2.

当有UITabbar时候需要需要重写对应的UITabBarController,首先要重写方法(BOOL)s houldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

 

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    UIViewController *controller = self.selectedViewController;

    if ([controller isKindOfClass:[UINavigationController class]])

        controller = [(UINavigationController *)controller visibleViewController];

    return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];

 

 

}

其次在需要翻转的视图控制器中重写方案1中的两个方法

方案3:

如果有UITabbar,当处于横向模式时候,需要隐藏UITabbar,就不能采用方案1中的方法:向当前视图添加子视图,而需要向UIWindow添加视图,同时还需要设置和调整其横向视图坐标位置,同时隐藏当前纵向模式视图。

 

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{

UIApplication*app=[UIApplication sharedApplication];

CGAffineTransform landscapeTransform;

if(toInterfaceOrientation!=UIInterfaceOrientationPortrait&&toInterfaceOrientation!= UIDeviceOrientationPortraitUpsideDown )

{

/*landscpecontroller view is the same level with self.view

both of them are the subview of the window. when we add the subview to

window, we can use the cgaffinetransform to chage the layout

*/

if(landScapeController==nil)

landScapeController=  [[PortfolioLandscape alloc] initWithNibName:@"PortfolioLandscape" bundle:nil];

landScapeController.colSortedTag=colSortedTag;

 

landScapeController.view.frame=CGRectMake(0, 0, 480, 320);

[[app keyWindow] addSubview:landScapeController.view];

app.statusBarHidden=YES;

self.tabBarController.view.hidden=true;

 

 

 

}

}

 

else 

{

 

self.tabBarController.view.hidden = false; 

app.statusBarHidden=NO;

}

 

 

 

 

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值