如何使导航栏与设备一起旋转

时间:2013-06-02 08:36:16

标签: ios uiviewcontroller uinavigationbar

我已按编程方式向我的应用添加了导航栏,如下所示:

    //Setup Navigationbar:
    navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [[UIScreen mainScreen ] bounds].size.width, 44)];

    UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:@"Navigation Title"];
    //Navigationbar left items:


    UIButton *fileButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [fileButton addTarget:self action:@selector(loadFile:) forControlEvents:UIControlEventTouchUpInside];
    [fileButton setImage:[UIImage imageNamed:@"menuicon.png"] forState:UIControlStateNormal];
    fileButton.frame = CGRectMake(0, 0, 24, 24);
    fileButton.backgroundColor=[UIColor clearColor];
    newItem = [[UIBarButtonItem alloc] initWithCustomView:fileButton];
   navBar.items = [NSArray arrayWithObjects: navigItem,nil];


    //NavigationBar appearance:
    [navBar setBackgroundImage:[UIImage imageNamed:@"greenbar.png"] forBarMetrics:UIBarMetricsDefault];



    [self.window addSubview: navBar];
    [self setTitle:@"Empty"];

问题是我不能让它随设备一起旋转,正如你所看到的那样,我将栏添加到我的AppDelegate的窗口。如何使其跟随旋转并自动调整大小?

1 个答案:

答案 0 :(得分:0)

要启用导航栏的旋转,需要将其添加到对设备旋转作出反应的视图中。我通过从[self.window addSubview: navBar];切换到[self.window.rootViewController.view addSubview: navBar];

来解决了这个问题