语境
我正在使用“initWithNavigationBarClass”方法使用自定义工具栏初始化 UINavigationController,这是我分配初始化 UINavigationController 的行
navigationController = [[UINavigationController alloc] initWithNavigationBarClass:nil toolbarClass:[QuestionToolbar class]];
是类,“QuestionToolbar”,我继承 UIToolbar 并覆盖 drawrect,这里是 drawRect 方法:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
UIImage *backgroundImage = [UIImage imageNamed:@"44px_background_red.png"];
[backgroundImage drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
这是 viewController 中的相关代码,我尝试在其中添加 UIBarButtonItems
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *beginItem = [[UIBarButtonItem alloc] initWithTitle:@"Begin Quiz" style:UIBarButtonItemStylePlain target:self action:@selector(beginAction:)];
[beginItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
NSArray *items = [NSArray arrayWithObjects:spacer, beginItem, spacer, nil];
[self.navigationController.toolbar setItems:items];
[self.navigationController setToolbarHidden:NO];
问题
我如何将 UIBarButtonItems 添加到此工具栏,因为当我尝试添加它们时它们不显示?
我认为这与我最重要的 drawRect 有关