11

我正在尝试以编程方式将 UINavigationBar 添加到 UIView 并取得了如此成功,但无法弄清楚。我正在尝试将它添加到我的 UIView 子类中,当我运行我的应用程序时它根本没有出现。

这是我的代码:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)];
    [navBar setBackgroundColor:[UIColor blueColor]];
    [self addSubview:navBar];

    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"];
    [navItem setRightBarButtonItem:doneItem animated:YES];
    [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];
4

5 回答 5

7

如果您从第一个视图中使用导航控制器,我建议您在 Appdelegate.m

 UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
        self.window.rootViewController=navController;

但是,如果您有一个显示您想要导航栏的新视图的视图,请使用它来显示新视图:

UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self presentViewController:navigationcontroller animated:YES completion:nil];

然后您可以在第二个视图中添加您的 navBarButtons,如下所示:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnAddGroupPressed)];
        self.navigationItem.rightBarButtonItem=btnAdd;

    }
    return self;
}
于 2013-08-27T19:30:52.227 回答
5

我不确定发生了什么事。这是我用来重现您的问题的代码:

*.h 文件为空

#import "STTestView.h"

@implementation STTestView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code

        UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
        navBar.barTintColor = [UIColor purpleColor];
        [self addSubview:navBar];

        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

        UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:@"categories"];
        [navItem setRightBarButtonItem:doneItem animated:YES];
        [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];

        self.backgroundColor = [UIColor darkGrayColor];
    }
    return self;
}

-(void)done:(id)sender {

}

@end

这就是我得到的:

在此处输入图像描述

请你:

  1. 添加整个initWithFrame:方法的代码?
  2. 确保在代码运行时 self.frame 和 headerHeight 实际上设置正确?
  3. 记住不要使用backgroundColor属性,而是使用barTintColor属性

希望这可以帮助!

于 2013-08-27T19:46:02.883 回答
3

我不能告诉你为什么,但我遇到过同样的问题,我追查到了setBackgroundColor方法。为了解决这个问题,我做了:

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 44.0)];
UINavigationItem *titleItem = [[UINavigationItem alloc] initWithTitle:@"MyNavBar"];
NSDictionary *titleAttributesDictionary =  [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],
                                            UITextAttributeTextColor,
                                            [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0],
                                            UITextAttributeTextShadowColor,
                                            [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
                                            UITextAttributeTextShadowOffset,
                                            [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0],
                                            UITextAttributeFont,
                                            nil];
navBar.titleTextAttributes = titleAttributesDictionary;
navBar.items = @[titleItem];
navBar.tintColor=[UIColor blueColor];
[self addSubview:navBar];
于 2013-08-27T19:47:07.983 回答
1

Perhaps, you forgot "navBar.items = @[navItem];" as I dealt with similar issue:

    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)];
    navBar.barTintColor = [UIColor blueColor];
    [self addSubview:navBar];//it is important point, otherwise, you can see only the area without any item

    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"done" style:UIBarButtonItemStylePlain target:self action:@selector(done:)];

    [navItem setRightBarButtonItem:doneItem animated:YES];
    [navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];

Use these properties: "barTintColor" to get the color of the navigation bar background, and "tintColor" for the navigation items and bar button items.

于 2015-07-04T07:26:57.277 回答
0

如果你使用的是 xib 而不是故事板,那么在 AppDelegate 中写下这些行:

UIViewController *viewControllerObj = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewControllerObj]; 
self.window.rootViewController=navController;

如果您使用的是故事板,那么从菜单中添加 NavigationController 会很好。

于 2016-04-27T21:32:53.433 回答