UISplitViewController 在尝试使用其中的 UINavigationController 后停止自动旋转

发布于 2024-10-09 13:19:59 字数 1143 浏览 0 评论 0原文

我在 iPad 应用程序中使用 UISplitViewController 时遇到了一些问题。我正在尝试使用 UISplitView 内部的 UINavigationController 制作一个简单的导航树。我使用了以下基本代码来执行此操作:

NavController.h

@interface NavController : NSObject {
    /* 
     * This is connected properly to the UINavigationController in the 
     * UISplitViewController through Interface Builder.
     */

    UINavigationController *navigationController;

 }

 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

 @end

NavController.m

#import "NavController.h"

@implementation NavController

@synthesize navigationController;

- (void) awakeFromNib {
    UIViewController *testController = [[UIViewController alloc] init];
    UITableView *tableView = [[UITableView alloc] init];

    [testController setView: tableView];

    [navigationController pushViewController: testViewController
                                    animated: YES];

}

@end

此代码成功将视图推送到导航控制器,并且我可以使用后退按钮导航回来,但是,我的问题出现了,因为在发生这种情况之后,我的 UISplitViewController 不再自动旋转或根本从纵向位置旋转。当我删除此代码(并且视图不会被推送)时,它会按预期工作。

我做错了什么,我是否以正确的方式处理这件事?

I've been having a bit of trouble with a UISplitViewController in my iPad app. I am attempting to make a simple navigation tree using the UINavigationController inside of the UISplitView. I have used the following basic code to do this:

NavController.h

@interface NavController : NSObject {
    /* 
     * This is connected properly to the UINavigationController in the 
     * UISplitViewController through Interface Builder.
     */

    UINavigationController *navigationController;

 }

 @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

 @end

NavController.m

#import "NavController.h"

@implementation NavController

@synthesize navigationController;

- (void) awakeFromNib {
    UIViewController *testController = [[UIViewController alloc] init];
    UITableView *tableView = [[UITableView alloc] init];

    [testController setView: tableView];

    [navigationController pushViewController: testViewController
                                    animated: YES];

}

@end

This code successfully pushes the view to the navigation controller, and I can navigate back with the back button, however, my problem arises with the fact that after this happens, my UISplitViewController no longer auto-rotates or rotates at all from the portrait position. When I remove this code (and the view does not get pushed) it works as expected.

What am I doing wrong, and am I going about this in the right way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金兰素衣 2024-10-16 13:19:59

这也让我彻底抓狂。我做了一些事情使它起作用,但我对我的解决方案并不满意 - 1)我不太理解它,2)它看起来很老套。

我将其添加到我的应用程序委托(我的 .m 文件)中:

@interface UITabBarController (MyApp)
@end

@interface UINavigationController (MyApp)
@end

@implementation UITabBarController (MyApp) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
@end

@implementation UINavigationController (MyApp) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
@end

这在很大程度上有效。对于不自动旋转的视图,我必须使用变换手动旋转视图。我做了类似的事情:

- (void)deviceOrientationDidChangeWithAnimation:(BOOL)animated {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (orientation == oldOrientation) {
        return;
    }

    if (animated) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        [UIView setAnimationDidStopSelector:@selector(orientationChanged)];
    }

    [self sizeToFitOrientation:YES];

    if (animated) {
        [UIView commitAnimations];
    }

    oldOrientation = orientation;
}

- (CGAffineTransform)transformForOrientation {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5); // rotated CCW
    } else if (orientation == UIInterfaceOrientationLandscapeRight) { // CW
        return CGAffineTransformMakeRotation(M_PI/2);
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { // CCW
        return CGAffineTransformMakeRotation(-M_PI);
    } else { // CW
        return CGAffineTransformIdentity;
    }
}

- (void)sizeToFitOrientation:(BOOL)transform {
    if (transform) {
        self.view.transform = CGAffineTransformIdentity;
    }

    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));

    CGFloat width = frame.size.width - 0 * 2;
    CGFloat height = frame.size.height - 0 * 2;

    UIInterfaceOrientation _orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(_orientation)) {
        self.view.frame = CGRectMake(0, 0, height, width);
    } else {
        self.view.frame = CGRectMake(0, 0, width, height);
    }
    self.view.center = center;

    if (transform) {
        self.view.transform = [self transformForOrientation];
    }
}

希望这有帮助!如果有人能指出我所犯的错误(或我正在犯的坏事),我会很乐意学习。 :)

This drove me absolutely nuts too. I did a couple of things that made it work, but I'm not happy about my solutions -- 1) I don't really understand it and 2) it seems hacky.

I added this to my app delegate (my .m file):

@interface UITabBarController (MyApp)
@end

@interface UINavigationController (MyApp)
@end

@implementation UITabBarController (MyApp) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
@end

@implementation UINavigationController (MyApp) 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return YES;
}
@end

This worked for the most part. For the views that didn't auto-rotate though, I had to manually rotate the views myself using transforms. I did something like:

- (void)deviceOrientationDidChangeWithAnimation:(BOOL)animated {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

    if (orientation == oldOrientation) {
        return;
    }

    if (animated) {
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        [UIView setAnimationDidStopSelector:@selector(orientationChanged)];
    }

    [self sizeToFitOrientation:YES];

    if (animated) {
        [UIView commitAnimations];
    }

    oldOrientation = orientation;
}

- (CGAffineTransform)transformForOrientation {
    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5); // rotated CCW
    } else if (orientation == UIInterfaceOrientationLandscapeRight) { // CW
        return CGAffineTransformMakeRotation(M_PI/2);
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) { // CCW
        return CGAffineTransformMakeRotation(-M_PI);
    } else { // CW
        return CGAffineTransformIdentity;
    }
}

- (void)sizeToFitOrientation:(BOOL)transform {
    if (transform) {
        self.view.transform = CGAffineTransformIdentity;
    }

    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGPoint center = CGPointMake(frame.origin.x + ceil(frame.size.width/2), frame.origin.y + ceil(frame.size.height/2));

    CGFloat width = frame.size.width - 0 * 2;
    CGFloat height = frame.size.height - 0 * 2;

    UIInterfaceOrientation _orientation = [UIApplication sharedApplication].statusBarOrientation;
    if (UIInterfaceOrientationIsLandscape(_orientation)) {
        self.view.frame = CGRectMake(0, 0, height, width);
    } else {
        self.view.frame = CGRectMake(0, 0, width, height);
    }
    self.view.center = center;

    if (transform) {
        self.view.transform = [self transformForOrientation];
    }
}

Hope this helps! And if someone can point out mistakes I've made (or bad things I'm perpetuating), I'd be happy to learn. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文