0

我以编程方式向我的应用程序添加了一个导航栏,如下所示:

    //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 的窗口中。我如何让它跟随旋转和自动调整大小?

4

1 回答 1

0

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

于 2013-06-02T09:02:06.467 回答