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

Jos*_*ane 11 objective-c uinavigationbar uiview ios

我试图以编程方式将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];
Run Code Online (Sandbox Code Playgroud)

Ely*_*ani 7

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

 UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
        self.window.rootViewController=navController;
Run Code Online (Sandbox Code Playgroud)

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

UIViewController *viewController = [[MenuViewController alloc] initWithNibName:@"MenuViewController" bundle:nil];
            UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
[self presentViewController:navigationcontroller animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

然后你可以在第二个视图中添加你的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;
}
Run Code Online (Sandbox Code Playgroud)


Lui*_*ien 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
Run Code Online (Sandbox Code Playgroud)

这就是我得到的:

在此输入图像描述

请你:

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

希望这可以帮助!