UIInterfaceOrientation over iOS6 (应用旋转屏幕)

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};

UIInterfaceOrientation enum

使App支持旋转

有两种方法:

1.选择Project->Target->General->Deployment Info->Device Orientation,勾选需要支持的屏幕方向,比如

Portrait

Upside Down

Landscape Left

Landscape Right

2.在AppDelegate.m文件中,添加方法

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskLandscape;
}

以上面中的设置为例

启动画面的方向取决于方法一,启动画面之后的Window方向取决于方法二,如果未实现方法二,则取决于方法一

大部分页面为Portrait,个别页面为Landscape

例如AppDelegate中的window的rootViewController为TabBarController,则TabBarController的ViewControllers的旋转方向都取决于TabBarController,因此需要在TabBarController中实现以下方法

#import "TYSXTabBarController.h"

#define kTabBarColor [UIColor colorWithRed:0.953f green:0.953f blue:0.953f alpha:1.00f]
#define kTintColor [UIColor colorWithRed:0.914f green:0.000f blue:0.294f alpha:1.00f]

@implementation TYSXTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tabBar.translucent = NO;
    [self.tabBar setBarTintColor:kTabBarColor];
    self.tabBar.tintColor = kTintColor;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}
@end

TabBarController.m

这样,TabBarController下的所有页面就都只能是竖向了。

如何让某个页面只能是横向的呢?

以全屏播放页面(FullScreenViewController)为例,Present显示该页面,如此,播放页面就与TabBarController独立开了,在FullScreenViewController.m中实现以下方法

@implementation FullScreenViewController

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

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

@end

FullScreenViewController.m

时间: 2024-08-05 11:30:00

UIInterfaceOrientation over iOS6 (应用旋转屏幕)的相关文章

iOS6 自适应旋转处理

一.iOS6之后UIViewController 旋转处理 1.view controller 是否支持自动旋转 - (BOOL)shouldAutorotate; 2.view controller 支持哪几种 - (NSUInteger)supportedInterfaceOrientations; 在UIApplication中定义了屏幕方向相关的枚举 UIInterfaceOrientationMask枚举类型 定义了支持的屏幕类型 UIInterfaceOrientation枚举类型

自动旋转屏幕默认旋转

如下图所示,在处理屏幕默认旋转方向的时候可以在这里进行选择,上下左右一共是4个方向. 策划的需求是游戏采用横屏,但是要求支持两个方向自动旋转,如下图所示,我的设置是这样的.Default Orientation* Auto Roation 表示游戏支持自动旋转屏幕,但是前提是手机没有锁定屏幕旋转功能.Landscape Right 和Landscape Left 表示手机屏幕只支持横屏两个方向的自动旋转. <ignore_js_op> 这里问题就来了,既然设置了两个方向的自动旋转,那么必然会有

Android——旋转屏幕导致Activity重建解决方法

Android开发文档上专门有一小节解释这个问题.简单来说,Activity是负责与用户交互的最主要机制,任何“设置”(Configuration)的改变都可能对Activity的界面造成影响,这时系统会销毁并重建Activity以便反映新的Configuration. “屏幕方向”(orientation)是一个Configuration,通过查看Configuration类的javadoc可以看到其他Configuration还有哪些:如fontScale.keyboardHidden和lo

android手机旋转屏幕时让GridView的列数与列宽度自适应

无意中打开了一年前做过的一个android应用的代码,看到里面实现的一个小功能点(如题),现写篇文章做个笔记.当时面临的问题是,在旋转屏幕的时候需要让gridview的列数与宽度能自适应屏幕宽度,每个单元格之间还需要保留一定的间距.因为每款手机的屏幕宽度不都相同,我们在指定了单元格的宽度与间距之后,并不能确定每行中所能容纳的单元格数量,这个数量必须在运行时通过计算得出,同样,我们设置的单元格宽度和间距不能保证刚好容纳在屏幕宽度内,为了解决这个问题,设计了一个简单的算法,首先需要预先指定单元格的宽

安卓开发技术分享:旋转屏幕导致Activity重建

Android开发文档上专门有一小节解释这个问题.简单来说,Activity是负责与用户交互的最主要机制,任何“设置”(Configuration)的改变都可能对Activity的界面造成影响,这时系统会销毁并重建Activity以便反映新的Configuration. “屏幕方向”(orientation)是一个Configuration,通过查看Configuration类的javadoc可以看到其他Configuration还有哪些:如fontScale.keyboardHidden和lo

关于Android旋转屏幕丢失数据的新读

根据之前的经验,一直以为当Android屏幕旋转的时候会重新调用onCreate(),从而导致界面上所有的数据都会被重置,需要在Manifest中对Activity设置一个属性才能让他不重新走onCreate方法.或者很麻烦的需要在 onSaveInstanceState()方法中保存界面所有数据,然后在onRestoreInstanceState()方法中还原数据才行. 直到今天看到这样一句话: "By default, the system uses the Bundle instance

Android 中旋转屏幕,触摸,滚动

package com.learingselenium.android; import junit.framework.TestCase import org.openqa.selenium.Rotatable; import org.openqa.selenium.ScreenOrientation; import org.openqa.selenium.WebDriver; import org.openqa.selenium.android.AndroidDriver; import or

单击Android设备后退键,主屏幕键以及旋转屏幕如何影响Activity的生命周期

单击设备的后退键,相当于通知Android系统“我已完成activity的使用,现在不需要它了.”接到指令后,系统立即销毁了activity.即调用onPause()->onStop()->onDestroy() 单击主屏幕键,相当于通知Android“我去别处看看,稍后可能回来.”此时,为快速响应随时返回应用,Android只是暂停当前activity而并不销毁它.即调用onPause()->onStop(),但是并没有调用onDestroy(). 注意:停止的activity能够存在

Cocos2dx 2.x 安卓重力检测 旋转屏幕

在充电的情况下,玩手机的时候,屏幕一般需要特定的旋转方向. 功能实现分成两部分:一部分根据手机重力方向X,Y,Z得出所需要的角度:另一方面根据旋转角度,设置屏幕旋转方向. 通过监听手机相对于X,Y,Z方向的值,算出绕着某一轴的角度.X,Y方向分别平行于手机界面,Z垂直于手机界面. 本文以绕Y轴旋转为例,如需绕Z轴只需把Y和Z互换,一般情况下只会要求这两种情况. 代码借鉴网上的. package com.gamemaster.orientation; import android.hardware