上下文
我正在使用' initWithNavigationBarClass'使用自定义工具栏初始化UINavigationController的方法,这里是我分配init 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
有关答案 0 :(得分:0)
我尝试使用自定义UIToolbar,问题不是来自-(void)drawRect:(CGCrect)rect
。
我不知道您是否尝试在UIToolbar上添加按钮,但您应该尝试在UIViewController类的-(void)viewDidAppear
方法中添加它们。这样它对我有用。