我正在尝试以编程方式将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];
if (self)
语句内的init方法中被调用。我不明白为什么这会阻止它工作,因为我在这里添加了其他子视图。 - Josh Kahane