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

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

你是在哪个方法中使用这段代码的?我刚试了一下,对我来说可以工作。另外,设置backgroundColor不会有任何效果。你必须使用"tintColor"属性。 - LuisCien
它在if (self)语句内的init方法中被调用。我不明白为什么这会阻止它工作,因为我在这里添加了其他子视图。 - Josh Kahane
嗯,这确实很奇怪,因为我刚刚尝试再次重现您的情况,它对我起作用了。我认为问题可能是“headerHeight”设置为0。 - LuisCien
忘记包含那个了。那个设置为44.0。 - Josh Kahane
"barTintColor"属性。 - Darius Miliauskas
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];

然后您可以像下面这样在第二个视图中添加导航栏按钮:

- (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;
}

谢谢您的建议,但我已经在问题中解释了。我正在尝试将UINavigationBar添加到UIView中。没有控制器涉及此问题。 - Josh Kahane

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属性。
希望这可以帮到您!

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

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"获取导航项和栏按钮项的颜色。

0
如果您使用xib而非Storyboard,则在AppDelegate中编写以下代码:
UIViewController *viewControllerObj = [[MenuViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewControllerObj]; 
self.window.rootViewController=navController;

如果您正在使用Storyboard,则最好从菜单中添加NavigationController。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接