首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS 6横向和纵向

iOS 6横向和纵向
EN

Stack Overflow用户
提问于 2012-09-24 20:37:52
回答 3查看 17.5K关注 0票数 4

我有一个表格,上面有一大串来自plist文件的东西,点击每一个都会把你带到一个新的视图,一个xib。

我在.xib里面有两个视图,一个是纵向视图,另一个是横向视图

在我的h文件中,我有以下内容:

代码语言:javascript
复制
IBOutlet UIView *portraitView;  
IBOutlet UIView *landscapeView;

@property (nonatomic, retain) IBOutlet UIView *portraitView;  
@property (nonatomic, retain) IBOutlet UIView *landscapeView;

在我的m文件中:

代码语言:javascript
复制
[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-如何确定故事板方向?(它们是静态的,现在一切都旋转了(根本没有代码))

谢谢。

EN

回答 3

Stack Overflow用户

发布于 2012-10-09 20:07:08

我想我有办法解决这个问题。它很难看,但它是有效的。对于iOS6,苹果建议现在使用2个不同的XIB文件在纵向和横向视图之间切换。

但是如果你想通过在2 UIView IBOutlet之间“切换”来使用之前在iOS 5.0中允许的方法,你可以尝试我的丑陋的工作解决方案。其想法是根据方向旋转视图。

1)在you viewDidLoad中,订阅定向通知:

代码语言:javascript
复制
- (void)viewDidLoad
{
    [super viewDidLoad];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
 }

2)添加通知调用的方法:

代码语言:javascript
复制
-(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)最后,为每个方向添加旋转方法:

代码语言:javascript
复制
-(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宏来在你的控制器中包含这两种代码。

干杯

票数 13
EN

Stack Overflow用户

发布于 2013-02-05 17:46:09

无需发送和接收通知:

在您的appdelegate.m中,使用以下方法

代码语言:javascript
复制
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window

总是被调用来检查窗口的方向,所以一种简单的方法是将下面描述的代码放在appdelegate.m中。

代码语言:javascript
复制
- (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;
}

并实现

代码语言:javascript
复制
- (NSUInteger)supportedInterfaceOrientations

在各自的视图控制器中

代码语言:javascript
复制
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait; //Or anyother orientation of your choice
}

要针对方向更改执行突然操作,请实现以下方法

代码语言:javascript
复制
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
票数 1
EN

Stack Overflow用户

发布于 2014-02-10 18:10:52

现在回答已经很晚了,不过我还是想和你分享一下,以防万一,

我也有同样的问题。

从iOS 6开始,shouldAutorotateToInterfaceOrientation已被弃用。

您需要将此方法与新的supportedInterfaceOrientationsshouldAutorotate方法进行比较。

这一点非常非常重要,您需要确保在应用程序委托的applicationDidFinishLaunching方法中设置视图根控制器,而不是简单地将视图控制器的视图(或导航控制器或tabBar控制器,取决于您使用的内容)作为子视图添加到窗口中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12565188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档