稍后在View Controller中更改UINavigationBar

时间:2015-06-06 15:26:28

标签: ios uinavigationbar

我有一个没有NavigationController的视图控制器(但不能)。我定义了这样的导航栏。

There is no South database module 'south.db.mysql' for your database. Please either choose a supported database, check for SOUTH_DATABASE_ADAPTER[S] settings, or remove South from INSTALLED_APPS.

如何在View Controller中更改背景? 以下不起作用。我想这是因为我没有NavigationController:

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];

    navBar.backgroundColor = UIColorFromRGB(0x72CE97);
    navBar.barTintColor = UIColorFromRGB(0x72CE97);

    //Keep the container for Navigation Items
    UINavigationItem *navItem = [[UINavigationItem alloc] init];
    UIImage* logoImage = [UIImage imageNamed:@"nav_logo_small.png"];
    navItem.titleView = [[UIImageView alloc] initWithImage:logoImage];

    //Add the left button as an Item to Navigation Items

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backButton:)];
    navItem.leftBarButtonItem = leftButton;

    //Add Navigation Items Container to Navigation Bar
    navBar.items = @[ navItem ];

    //Display the Navigation Bar
    [self.view addSubview:navBar];
    [navBar release];

1 个答案:

答案 0 :(得分:1)

试试这个:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.tag = 1;
...
NavigationBar *navigationBar = (NavigationBar *)[self.view viewWithTag:1];