以编程方式将UINavigationBar添加到UIView?

时间:2013-08-27 19:11:55

标签: ios objective-c uiview uinavigationbar

我正在尝试以编程方式将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];

5 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分: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

这就是我得到的:

enter image description here

你能取悦:

  1. 添加整个initWithFrame:方法的代码?
  2. 确保在代码运行时正确设置了self.frame和headerHeight吗?
  3. 请记住,不要使用backgroundColor属性,而应使用barTintColor属性
  4. 希望这有帮助!

答案 2 :(得分: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];

答案 3 :(得分:1)

也许,你忘了“navBar.items = @ [navItem];”因为我处理了类似的问题:

    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];

使用以下属性:“barTintColor”获取导航栏背景的颜色,使用“tintColor”获取导航项和条形按钮项。

答案 4 :(得分:0)

如果您使用的是xib而不是storyboard,请在AppDelegate中写下这些行:

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

如果您正在使用故事板,那么从菜单中添加NavigationController是件好事。