代码改变世界

iphone处理翻转 横屏代码

2012-02-22 11:47  java环境变量  阅读(180)  评论(0编辑  收藏  举报
方案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 < src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"> < src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"> 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;

}