簡體   English   中英

UINavigationBar沒有旋轉

[英]UINavigationBar is not rotating

我遇到一個小問題。 我正在嘗試使用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類?

很抱歉閱讀了很久,感謝您提供的任何幫助

您應該將導航控制器設置為窗口的根視圖控制器。 另外,在applicationDidBecomeActive:method中,您具有以下這一行:[navigation pushViewController:viewController animation:NO]; 在實例化導航之前使用的代碼,因此該行沒有任何作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM