UINavigationBar不旋转

时间:2012-09-05 15:22:23

标签: objective-c ios xcode rotation uinavigationbar

我遇到了一个小问题。我正在尝试使用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

所以我想知道是否实际上可以旋转NavigationBar。我也创建它应该创建它? 我应该以这种方式启动我的UINavigationController,还是应该在IB中创建它并将其链接到.h类?

很抱歉阅读不久,感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:1)

您应该使导航控制器成为窗口的根视图控制器。另外,在你的applicationDidBecomeActive:方法中,你有这一行:[navigation pushViewController:viewController animated:NO];您在实例化导航之前使用的,因此该行没有做任何事情。