ios屏幕旋转

旧项目在iOS6中无法旋转的解决办法

之前做的一个iPad上的app,在最新的sdk下重新编译,出现无法旋转的情况。查看了一些文档,由于iOS6在旋转处理的api有了变化,之前处理旋转的函数shouldAutorotateToInterfaceOrientation:在iOS6下无效,由supportedInterfaceOrientations 和 shouldAutorotate替代。

于是在对应的viewcontroller上增加了这两个函数,但还是无效。函数有响应,但没达到旋转的效果。

继续查找一些资料,在一篇文章中找到原因,由于旧的项目没有rootViewController,而iOS6判断是否需要旋转是根据rootViewController来判断的。

于是在代码中手动添加self.window.rootViewController=xxxViewController,(xxxViewController为启动的主界面)即可解决。

详细可参考以下文章:

http://blog.csdn.net/bl1988530/article/details/8641099#t1

内容转载如下:

如果你的 iOS App 支持随屏幕旋转而旋转,但是到了 iOS 6 中,发现这个功能不生效了,那么就要检查一下是不是因为旧版本的 Xcode 创建工程的原因。
但是在 iOS SDK Release Notes for iOS 6.0 中的 UIKit 一节中说到“Autorotation is changing in iOS 6. In iOS 6, the shouldAutorotateToInterfaceOrientation: method of UIViewController is deprecated. In its place, you should use the supportedInterfaceOrientationsForWindow: and shouldAutorotate methods.”,并且详细解释中也说明了 iOS 6 中的改动:

[html]  view plain copy
  1. More responsibility is moving to the app and the app delegate. Now, iOS containers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom.  
  2. A view controller’s supported interface orientations can change over time—even an app’s supported interface orientations can change over time. The system asks the top-most full-screen view controller (typically the root view controller) for its supported interface orientations whenever the device rotates or whenever a view controller is presented with the full-screen modal presentation style. Moreover, the supported orientations are retrieved only if this view controller returns YES from its shouldAutorotate method. The system intersects the view controller’s supported orientations with the app’s supported orientations (as determined by the Info.plist file or the app delegate’s application:supportedInterfaceOrientationsForWindow: method) to determine whether to rotate.  
  3. The system determines whether an orientation is supported by intersecting the value returned by the app’s supportedInterfaceOrientationsForWindow: method with the value returned by the supportedInterfaceOrientations method of the top-most full-screen controller.  
  4. The setStatusBarOrientation:animated: method is not deprecated outright. It now works only if the supportedInterfaceOrientations method of the top-most full-screen view controller returns 0. This makes the caller responsible for ensuring that the status bar orientation is consistent.  
  5. For compatibility, view controllers that still implement the shouldAutorotateToInterfaceOrientation: method do not get the new autorotation behaviors. (In other words, they do not fall back to using the app, app delegate, or Info.plist file to determine the supported orientations.) Instead, the shouldAutorotateToInterfaceOrientation: method is used to synthesize the information that would be returned by the supportedInterfaceOrientations method.  
说明中提到,在 iOS 6 中系统会通过查询 rootViewController 来判断是否可以旋转视图,但是在以前,使用 Xcode 创建一个工程时,通常会有以下代码来显示主窗口:
[cpp]  view plain copy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  2.     // Add the tab bar controller's view to the window and display.  
  3.     [self.window addSubview:tabBarController.view];  
  4.     [self.window makeKeyAndVisible];  
  5.    
  6.     return YES;  
  7. }  
在这里,并没有设置 self.window 的 rootViewController,而且在 MainWindow.xib 中,也没在连接 window 的 rootViewController 到默认的 UITabBarController。

因此,我们有两种方法来解决这个问题:

1. 在代码中设置 rootViewController
[cpp]  view plain copy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  2.     // Add the tab bar controller's view to the window and display.  
  3.     self.window.rootViewController = tabBarController;  
  4.     [self.window addSubview:tabBarController.view];  
  5.     [self.window makeKeyAndVisible];  
  6.    
  7.     return YES;  
  8. }  
2. 在 MainWindow.xib 中连接 window 的 rootViewController。


UIViewController没有随着设备一起旋转的原因

对于iPhone app,UIViewController类提供了基本的视图管理模式。当设备改变方向的时候view controller的视图会自动随之旋转的。如果视图和子视图的autoresizing属性设置是对的,这时候视图又没有随着设备一起旋转,可能是以下的原因:


1.view controller没有完成代理方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;

也要实现了shouldAutorotateToInterfaceOrientation方法,同时shouldAutorotateToInterfaceOrientation方法要返回YES来支持所有的旋转方向

 

2.view controller的UIView属性嵌入在UIWindow中,并非是一个附加的view controller

你可能会发现shouldAutorotateToInterfaceOrientation方法只会在view controller启动的时候被调用,不会因为设置的旋转而在被调用。这是因为view controller束缚着他们所管理的视图,view controller是用来处理时间的响应链的一部分。view controller是从UIResponder继承而来,同时他被插入到他所管理的视图和他的superview之间。因此,通常的做法是在你的app中有一个主view controller来作为响应链的一部分。通常来说会添加一个主view controller,例如UINavigationController, UITabBarController或者UIViewController到UIWindow上。

例如

[myWindow addSubview:primaryViewController.view]; 

如果你添加了另外一个view controller的UIView属性到UIWindow(anotherController和主view controller在同一个等级上)

[myWindow addSubview:anotherController.view];

anotherController将不会接受旋转事件,只有第一个view controller(primaryViewController)会接受旋转事件。

 

3.你添加了view controller的UIView属性到UIWindow作为subview,但是过早的release它。

UIWindow会retain视图,而不是view controller。你不能过早的release他。在UIApplicationDelegate子类中定义他为retain属性。

 

4.在UITabBarController或者UINavigationController中的子view controller没有对同一方向的支持。

为了确保所有的子view controller旋转正确,你的每一个view controller,每一个tab或者额navigation都要完成shouldAutorotateToInterfaceOrientation,而且必须支持对于同一方向的旋转,也就是说对于同一方向的位置要返回为YES。

 

5.重写-(id)init:或者 -(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle 方法的时候没有调用super。

对于对象的初始化,在你的view controller的init或者initWithNibName方法中必须要调用super。

/

音频视频播放:http://blog.sina.com.cn/s/blog_6f1a34260100tlvx.html

////////转载

 iphone提供了优秀的重力加速计,能够很灵敏地感应到手机的屏幕状态。在我们开发iphone应用时,为了提高用户体验,会来监听屏幕的当前状态。
对于UIViewController来说,提供了好几种方法。

    最简单的就是我们可以使用Interface Builder这个工具来帮助我们实现简单的屏幕界面适配,只需要在IB 中的Size Insepector 中通过对Autosizing 的
设置来实现。但这只能做到简单界面的实现,如果界面比较复杂,可能就需要我们通过代码来实现。

    第二个我们可以通过对 willAnimateRotationToInterfaceOrientation:duration:这个方法的重写来配置我们的自动旋转。

    第三,你也可以通过对willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:与willAnimateSencondHalfOfRotationToInterfaceOrientation:duration:两个方法的重写来更加精细地配置我们的自动旋转视图。特别需要注意 的就是前一个方法在执行时,此时的UIInterfaceOrientation仍然是旋转前的原有值,只有后一个方法在执行时UIInterfaceOrientation才是屏幕旋转后的值。

UINavigationController或UITabbarController没有随着设备一起旋转的原因

UINavigationController

在ios6中如果要实现设备“屏幕旋转”的效果,需要在该Controller中使用以下三个方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//ios<6.0
- ( BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
        //return ;
}
 
//ios>6.0
- ( BOOL )shouldAutorotate
{
        //return;
}
- ( NSUInteger )supportedInterfaceOrientations
{
    // return
}

但如果想要控制“UINavigationController”直接在Contoller中使用这三个方法,将会不起作用,原因是ios6中“UINavigationController”并不支持- (BOOL)shouldAutorotate、- (NSUInteger)supportedInterfaceOrientations个方法,如果您想要控制其旋转,必须为其添加一个category,使其支持这个三个方法。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@implementation UINavigationController (SupportIOS6)
 
 
-( BOOL )shouldAutorotate
{
     return [[ self .viewControllers lastObject] shouldAutorotate];
}
 
-( NSUInteger )supportedInterfaceOrientations
{
     return [[ self .viewControllers lastObject] supportedInterfaceOrientations];
}
 
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
     return [[ self .viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
@end

这样,就可以在很方便的调用这两个方法控制其屏幕旋转了。


UITabbarController

今天遇到一个很郁闷的问题:新做的ipad程序中,界面居然在未作任何配置的情况下不支持旋转了。

通过在uitabbarcontroller的旋转事件添加nslog发现,其完全没有被触发。

通过网上查资料 在cocochina上找到了答案

   是位hanzengbo 的朋友的解答 原文如下

昨天忙乎了一天,没搞定。今天早上终于搞定了。贴出来希望能对遇到相似问题的哥们有用。前面搜到的答案都说是uitabbarcontroller的问题。但是我重新写了个小程序,也使用了tabbar这个控件,发现每个窗体也是能触发旋转事件的。昨天在tabbarcontroller这个地方弄了一天,一直没有弄成功。但是有一点可以肯定,就是旋转事件是被父窗体劫持了,从而使得tabbar中的每个窗体不能接受到旋转事件。今天来重新看了一下程序,发现问题在于我的程序开始有个启动窗体,在delegate文件中,我把tabbarcontroller的view加进了这个启动窗体,所以整个程序的父窗体就是这个启动窗体。我在启动窗体的旋转事件中打印log信息,能够成功。为了让tabbar的每个view能够响应旋转事件,就只能让他们成为单独的父窗体。所以我修改启动窗体代码,首先在delegate中addsubview 启动窗体,然后addsubview tabbar的窗体,在启动画面显示一定时间后,就把启动窗体从window中remove掉,这样下面的tabbar窗体就显示出来了,而且tabbar的窗体成为了父窗体,能够正常接受到旋转时间。

 

和我一样,我也有登录窗 嘿嘿 太巧了 

[[[self.view.superview subviews] objectAtIndex:0] removeFromSuperView];

搞定 。

界面正常了 。ios要学的东西真很多,为自己鼓劲 特此记录


注:

 

要翻转的时候,首先响应的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES则支持翻转,NO则不支持。

紧接着

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

被调用。这个方法是发生在翻转开始之前。一般用来禁用某些控件或者停止某些正在进行的活动,比如停止视频播放。
再来是

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

这个方法发生在翻转的过程中,一般用来定制翻转后各个控件的位置、大小等。可以用另外两个方法来代替:willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:   和  willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:

最后调用的是

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

这个方法发生在整个翻转完成之后。一般用来重新启用某些控件或者继续翻转之前被暂停的活动,比如继续视频播放。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值