【问题标题】:Adding UINavigationBar to UIView Programmatically?以编程方式将 UINavigationBar 添加到 UIView?
【发布时间】:2013-08-27 19:11:54
【问题描述】:

我正在尝试以编程方式将 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”属性
  • 它在 if (self) 语句中的 init 方法期间被调用。我不明白为什么这会阻止它工作,因为我在这里添加了其他子视图。
  • 嗯,这确实很奇怪,因为我刚刚再次尝试重现您的场景并且它对我有用。我相信问题可能是 'headerHeight' 设置为 0。
  • 忘了包括那个。设置为 44.0。
  • "barTintColor"属性。

标签: ios objective-c uiview uinavigationbar


【解决方案1】:

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

【讨论】:

  • 感谢您的建议,但我已在问题中进行了解释。我正在尝试将 UINavigationBar 添加到 UIView。没有控制器是此问题的一部分。
【解决方案2】:

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

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

    【讨论】:

      【解决方案4】:

      也许,您忘记了“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”获取导航项和栏按钮项。

      【讨论】:

        【解决方案5】:

        如果您使用的是 xib 而不是情节提要,请在 AppDelegate 中编写这些行:

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

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

        【讨论】:

          猜你喜欢
          • 2014-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-19
          • 2016-03-06
          • 1970-01-01
          • 2019-07-25
          相关资源
          最近更新 更多