Ios6 iOS 6横向和纵向定向
我有一个表,其中有一个来自plist文件的大列表,单击其中的每一个将带您进入一个新视图,即xib 我在里面有两个视图。xib,一个用于纵向视图,一个用于横向视图 在我的h文件中,我有以下内容:Ios6 iOS 6横向和纵向定向,ios6,device-orientation,xcode4.5,Ios6,Device Orientation,Xcode4.5,我有一个表,其中有一个来自plist文件的大列表,单击其中的每一个将带您进入一个新视图,即xib 我在里面有两个视图。xib,一个用于纵向视图,一个用于横向视图 在我的h文件中,我有以下内容: IBOutlet UIView *portraitView; IBOutlet UIView *landscapeView; @property (nonatomic, retain) IBOutlet UIView *portraitView; @property (nonatomic, re
IBOutlet UIView *portraitView;
IBOutlet UIView *landscapeView;
@property (nonatomic, retain) IBOutlet UIView *portraitView;
@property (nonatomic, retain) IBOutlet UIView *landscapeView;
在我的m文件中:
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (void) orientationChanged:(id)object
{
UIInterfaceOrientation interfaceOrientation = [[object object] orientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view = self.portraitView;
}
else
{
self.view = self.landscapeView;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsPortrait(interfaceOrientation)) {
self.view = portraitView;
} else if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
self.view = landscapeView;
}
return YES;
}
@end
在iOS 5中,一切都很完美,需要时可以显示风景或肖像
现在随着iOS 6的更新,一切都变得一团糟
如果我在表格(纵向)视图中并单击一个项目,则纵向显示正确,如果我旋转到横向,视图也显示正确的视图,但在横向中,如果我返回表格并选择另一个项目,则显示纵向视图而不是横向
如果我也这样做,但从风景开始,它会显示肖像
所以,现在的方向是没有任何工作
使用故事板的其他视图也会发生同样的情况。他们是肖像,总是这样显示,现在他们旋转,缩小一切,留下我的应用程序作为垃圾
1-如何修复.xib定向问题?2-如何确定故事板的方向?(它们是静态的,现在一切都在旋转(根本没有代码))
谢谢。我想我还有工作要做。它很难看,但它在工作。。。 对于iOS6,苹果现在建议使用2个不同的XIB文件在纵向和横向视图之间切换 但是,如果您想使用iOS5.0中允许的前一种方法,在2个UIView IBOutlet之间“切换”,您可以尝试我的丑陋的工作解决方案。其思想是根据方向旋转视图 1) 在viewDidLoad中,订阅方向通知:
-(void)viewDidLoad
{
[超级视图下载];
[[UIDevice currentDevice]BegingeratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(方向更改:)名称:@“UIDeviceOrientationIDChangeNotification”对象:nil];
}
2) 添加通知调用的方法:
-(void)方向更改:(NSNotification*)对象{
NSLog(“方向变化”);
UIDeviceOrientation deviceOrientation=[[object]定向];
if(deviceOrientation==UIInterfaceOrientationGraphital | | deviceOrientation==UIInterfaceOrientationGraphitoUpsideDown){
self.view=self.potraitView;
if(deviceOrientation==UIInterfaceOrientationGraphicalUpsideDown){
NSLog(@“将方向更改为向上向下”);
[自画像上下方向];
}否则{
NSLog(“将方向更改为纵向”);
[自画像定向];
}
}否则{
self.view=self.landscapeView;
if(deviceOrientation==UIInterfaceOrientationAndscapeLeft){
NSLog(@“方向改变为横向左侧”);
[自我景观定位];
}否则{
NSLog(@“方向改为横向右侧”);
[自我景观定位];
}
}
}
3) 最后,为每个方向添加旋转方法:
-(空)景观左方向{
//旋转视图。
CGAffineTransform transform=CGAffineTransformMakeRotation(-3.14159/2));
self.view.transform=转换;
//重新定位视图并调整其大小。
CGRect contentRect=CGRectMake(0,0480320);
self.view.bounds=contentRect;
}
-(空)景观方向{
//旋转视图。
CGAffineTransform transform=CGAffineTransformMakeRotation(3.14159/2);
self.view.transform=转换;
//重新定位视图并调整其大小。
CGRect contentRect=CGRectMake(0,0480320);
self.view.bounds=contentRect;
}
-(无效)方向{
//旋转视图。
CGAffineTransform transform=CGAffineTransformMakeRotation(0);
self.view.transform=转换;
//重新定位视图并调整其大小。
CGRect contentRect=CGRectMake(0,0320480);
self.view.bounds=contentRect;
}
-(无效)上下方向{
//旋转视图。
CGAffineTransform transform=CGAffineTransformMakeRotation(3.14159);
self.view.transform=转换;
//重新定位视图并调整其大小。
CGRect contentRect=CGRectMake(0,0320480);
self.view.bounds=contentRect;
}
我建议您创建一个自定义UIViewController类,并从该类继承ate以保存冗余代码。
如果您想同时支持ios5和ios6的解决方案,可以使用#endif宏在控制器中包含这两个代码
干杯无需发送和接收通知: 在appdelegate.m中,使用以下方法
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
始终调用以检查窗口的方向,
因此,一个简单的解决方法是在appdelegate.m中使用下面描述的代码
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
NSUInteger orientations = UIInterfaceOrientationMaskPortrait;
if(self.window.rootViewController){
UIViewController *presentedViewController ;
if ([self.window.rootViewController isKindOfClass:([UINavigationController class])])
{
presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
}
else if ([self.window.rootViewController isKindOfClass:[UITabBarController class]]){
UITabBarController *controller = (UITabBarController*)self.window.rootViewController;
id selectedController = [controller presentedViewController];
if (!selectedController) {
selectedController = [controller selectedViewController];
}
if ([selectedController isKindOfClass:([UINavigationController class])])
{
presentedViewController = [[(UINavigationController *)selectedController viewControllers] lastObject];
}
else{
presentedViewController = selectedController;
}
}
else
{
presentedViewController = self.window.rootViewController;
}
if ([presentedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
orientations = [presentedViewController supportedInterfaceOrientations];
}
}
return orientations;
}
实施
- (NSUInteger)supportedInterfaceOrientations
在相应的视图控制器中
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice
}
要针对方向变化执行突然动作,请执行以下方法
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
现在回答这个问题已经很晚了,但我还是觉得我应该和你分享一下,以防万一 我也有同样的问题 shouldAutorotateToInterfaceOrientation在iOS 6之后不推荐使用 您需要将此方法与新的支持的接口方向和应自动旋转方法并行 这一点非常重要,您需要确保在应用程序代理的ApplicationIDFinishLaunching方法中设置根控制器,而不是简单地添加视图控制器的视图(或导航控制器或tabBar控制器,具体取决于您使用的内容)作为窗口的子视图。