iOS 6风景和人像取向
我有一张桌子上有一大堆来自plist文件的东西,然后点击其中每个人都会将您带到一个新视图xib。iOS 6风景和人像取向
我有一个的.xib内2次,一个纵向和一个景观
在我的.h文件我有这样的:
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文件之间切换肖像&风景视图。
但是,如果您想通过2个UIView IBOutlet之间的“切换”来使用iOS 5.0中允许的前一个方法,那么您可以尝试我的难看的工作解决方案。这个想法是根据方向旋转视图。
1)在您的viewDidLoad,订阅取向通知:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
2)添加一个由通知调用的方法:
-(void)orientationChanged:(NSNotification *)object{
NSLog(@"orientation change");
UIDeviceOrientation deviceOrientation = [[object object] orientation];
if(deviceOrientation == UIInterfaceOrientationPortrait || deviceOrientation == UIInterfaceOrientationPortraitUpsideDown){
self.view = self.potraitView;
if(deviceOrientation ==UIInterfaceOrientationPortraitUpsideDown){
NSLog(@"Changed Orientation To PortraitUpsideDown");
[self portraitUpsideDownOrientation];
}else{
NSLog(@"Changed Orientation To Portrait");
[self portraitOrientation];
}
}else{
self.view = self.landscapeView;
if(deviceOrientation ==UIInterfaceOrientationLandscapeLeft){
NSLog(@"Changed Orientation To Landscape left");
[self landscapeLeftOrientation];
}else{
NSLog(@"Changed Orientation To Landscape right");
[self landscapeRightOrientation];
}
}
}
3)最后,加旋转方法对于每个方向:
-(void)landscapeLeftOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(-(3.14159/2));
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
-(void)landscapeRightOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159/2);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 480, 320);
self.view.bounds = contentRect;
}
-(void)portraitOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(0);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 320, 480);
self.view.bounds = contentRect;
}
-(void)portraitUpsideDownOrientation{
// Rotates the view.
CGAffineTransform transform = CGAffineTransformMakeRotation(3.14159);
self.view.transform = transform;
// Repositions and resizes the view.
CGRect contentRect = CGRectMake(0, 0, 320, 480);
self.view.bounds = contentRect;
}
我建议你做一个自定义的UIViewController类,并继承e从这个类节省冗余代码。 如果你想同时支持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以上版本。
您需要该并联方法与新supportedInterfaceOrientations和shouldAutorotate方法。
而且这是非常非常重要的,你需要确保你的应用程序委托的的applicationDidFinishLaunching方法,而不是简单地把视图控制器的视图设置根控制器(或导航控制器或的TabBar控制器根据在你正在使用的)作为窗口的子视图。