我遇到了一个小问题。我正在尝试使用 UINavigationController 栏开发一个应用程序,但它没有旋转,尽管我的 tableView 是。简单介绍一下,让您知道我现在的应用程序是什么:我有一个在 IB 中创建并在我的类 ViewController.h 中链接的 UITableView 和在 appDelegate.h 中创建的 UINavigationController。tableView 中的所有项目都可以点击,它会滑动到另一个 viewController 但这部分现在并不重要。我现在只是想在主视图中支持旋转。
我在我的 AppDelegate.h 中创建 UINavigationController,如下所示:
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigation;
}
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UIViewController *viewController;
然后在 AppDelegate.m 我这样做:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
ViewController *viewController = [[ViewController alloc] init];
viewController.title = @"Agrinuvo";
[navigation pushViewController:viewController animated:NO];
navigation = [[UINavigationController alloc] initWithRootViewController:viewController];
navigation.navigationBar.tintColor = [UIColor darkGrayColor];
[[UIBarButtonItem appearance] setTintColor:[UIColor orangeColor]];
[_window addSubview:navigation.view];
}
我在我的 ViewController.m viewDidLoad 方法中尝试了这些:
self.navigationController.navigationBar.autoresizesSubviews = YES;
self.navigationController.navigationBar.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
我可以调整 UINavigationBar 的大小,但我无法终生旋转它(仍在 ViewController.m 中):
-(void) orientationChanged
{
CGRect frame = self.navigationController.navigationBar.frame;
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)
{
self.view.transform = CGAffineTransformMakeRotation(0);
frame.size.height = 44;
frame.origin.y = 15;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * 1);
frame.size.height = 44;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * -0.5);
frame.size.height = 32;
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
{
self.view.transform = CGAffineTransformMakeRotation(M_PI * 0.5);
frame.size.height = 32;
}
self.navigationController.navigationBar.frame = frame;
}
啊,是的,我也在我的所有控制器中尝试了这种方法,它在所有控制器中都返回 YES,但 NavigationBar 仍然没有旋转。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
所以我想知道是否真的可以旋转导航栏。我也像应该创建它一样创建它吗?我应该以这种方式启动我的 UINavigationController 还是应该在 IB 中创建它并将其链接到 .h 类?
很抱歉长时间阅读,感谢您提供的任何帮助