iOS 6横向和纵向 [英] iOS 6 landscape and portrait orientation
问题描述
我有一个表格,其中列出了来自plist文件的大量内容,点击每个文件会将您带到一个新视图,即xib。
I have a table with a big list of stuff that comes from a plist file and clicking each of them takes you to a new view, a xib.
I在.xib中有2个视图,一个用于肖像,一个用于横向
I have 2 views inside that .xib, one for portrait and one for landscape
在我的文件中我有这个:
In my h file I have this:
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中完美运行,在需要时显示横向或纵向。
Everything was working perfectly in iOS 5, showing landscape or portrait when needed.
现在使用iOS 6更新一切都很糟糕。
Now with the iOS 6 update everything is a mess.
如果我在表(肖像)视图中并单击一个项目,它在纵向中显示正确,如果我旋转到横向,视图也显示正确的视图,但如果我回到横向,表格并选择另一个项目,它显示纵向视图而不是横向。
If I am in the table (portrait) view and click one item, it shows correct in portrait, if I rotate to landscape, the view shows the correct view as well, BUT being in landscape, if I go back to the table and select another item, it shows the portrait view instead of the landscape.
如果我执行相同但开始横向,则显示纵向。
If I do the same but starting landscape, it shows portrait.
所以,现在方向不适合任何事情。
So, now the orientation is not working for anything.
使用storyboard的其他视图也是如此。他们是肖像,总是这样显示,现在他们轮换,缩小所有内容并将我的应用程序留作垃圾。
The same happens to my other views using storyboard. They are portrait and always showed like that, now they rotate, shrink everything and leave my app as trash.
1-如何修复.xib定位的事情?< br>
2-如何修复故事板方向? (它们是静态的,现在一切都旋转了(根本没有代码))
1- How can I fix the .xib orientation thing ?
2- How can I fix the storyboard orientation ? (they were static, now everything rotates (no code at all))
谢谢。
推荐答案
我认为我有一个解决方法。这很丑陋但是它正在工作...
iOS6 Apple建议现在使用2个不同的XIB文件在肖像和放大器之间切换景观视图。
I think that I have a work around. It's ugly but it's working... With iOS6 Apple suggest now to use 2 differences XIB file to switch between portrait & landscape view.
但是如果你想通过在两个UIView IBOutlet之间切换来使用iOS 5.0中允许的先前方法,你可以尝试我丑陋的工作解决方案。我们的想法是根据方向旋转视图。
But if you want to use the previous method allowed in iOS 5.0 by "switching" between 2 UIView IBOutlet, you can try my ugly working solution. The idea is to rotate the view according to the orientation.
1)在viewDidLoad中,订阅方向通知:
1) In you viewDidLoad, subscribe to orientation notification:
- (void)viewDidLoad
{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
}
2)添加通知调用的方法:
2) Add a method called by the notification:
-(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)最后,为每个方向添加旋转方法:
3) And finally, add rotation method for each orientation:
-(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类并从此类继承保存冗余代码。
如果你想同时支持ios5和ios6的解决方案,你可以使用#endif宏在你的控制器中包含这两个代码。
I suggest you to make a custom UIViewController class and inherit-ate from this class to save redundant code. If you want to support both solution for ios5 and ios6 you can use a #endif macro to include the both code in your controllers.
干杯
这篇关于iOS 6横向和纵向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!