gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:28:02 24 4
gpt4 key购买 nike

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

导航 Controller .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

导航 Controller .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

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

我哪里做错了,我的处理方式是否正确?

最佳答案

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

我将此添加到我的应用委托(delegate)(我的 .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

这在大多数情况下都有效。对于没有自动旋转的 View ,我必须自己使用转换手动旋转 View 。我做了类似的事情:

- (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];
}
}

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

关于cocoa-touch - UISplitViewController 在尝试使用其中的 UINavigationController 后停止自动旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4544023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com