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

Ios 以编程方式将UINavigationBar添加到UIView?,ios,objective-c,uiview,uinavigationbar,Ios,Objective C,Uiview,Uinavigationbar,我试图以编程方式将UINavigationBar添加到UIView中,并且取得了很大成功,但我无法理解它。我试图将它添加到UIView子类中,但在运行应用程序时它根本没有显示出来 这是我的密码: UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, headerHeight)]; [navBar setBackgroundColor:

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

如果您是从第一个视图使用导航控制器,我建议您在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;
}

我不知道发生了什么事。下面是我用来重现您的问题的代码:

*.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
这就是我得到的:

请你:

  • 为整个
    initWithFrame:
    方法添加代码
  • 确保在代码运行时,self.frame和headerHeight已正确设置
  • 记住不要使用
    backgroundColor
    属性,而是使用
    barTintColor
    属性

  • 希望这有帮助

    我无法告诉您原因,但我也遇到过同样的问题,我将其追溯到
    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];
    
    在我处理类似问题时,您可能忘记了“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”获取导航项和栏按钮项的颜色。

    如果您使用的是xib而不是故事板,请在AppDelegate中编写以下行:

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

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

    您使用什么方法获得此代码?我刚试过,它对我有效。此外,设置背景色也不会产生任何效果。您必须在
    if(self)
    语句中使用init方法期间调用的“tintColor”属性。我不明白为什么它会停止工作,因为我在这里添加了其他子视图。好吧,这确实很奇怪,因为我刚刚再次尝试重现您的场景,它对我有效。我相信问题可能是“headerHeight”设置为0。忘记了包括它。设置为44.0,“barTintColor”属性。谢谢你的建议,但我已经在问题中解释过了。我正在尝试将UINavigationBar添加到UIView。没有控制器是此问题的一部分。