子类化UIToolbar并覆盖drawRect:-不显示UIBarButtonItems [英] Subclassing UIToolbar and overriding drawRect: - UIBarButtonItems NOT displaying

查看:99
本文介绍了子类化UIToolbar并覆盖drawRect:-不显示UIBarButtonItems的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

我正在使用'initWithNavigationBarClass'方法来使用自定义工具栏初始化UINavigationController,这是我在其中的一行分配初始化UINavigationController

I'm using the 'initWithNavigationBarClass' method to initialize a UINavigationController with a custom toolbar, here is the line where I alloc init the UINavigationController

navigationController = [[UINavigationController alloc] initWithNavigationBarClass:nil toolbarClass:[QuestionToolbar class]];

是 QuestionToolbar类,我将UIToolbar子类化并覆盖drawrect,这是drawRect方法:

Is the class, "QuestionToolbar", I subclass UIToolbar and override drawrect, here is the drawRect method:

    - (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

Here is pertinent code in the viewController where I attempt to add the 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到此工具栏,因为当我尝试添加UIBarButtonItems时未显示它们?

How do I go about adding UIBarButtonItems to this toolbar as they don't show-up when I try to add them?

我认为这与我最重要的drawRect有关

I assume it's something to do with my overriding drawRect

推荐答案

我用自定义UIToolbar尝试了一下,问题不是出自-(void)drawRect:(CGCrect)rect

I tried myself with a custom UIToolbar and the problem is not from -(void)drawRect:(CGCrect)rect.

我不知道您在哪里尝试在UIToolbar上添加按钮,但是应该尝试在-(void)viewDidAppear中添加按钮您的UIViewController类的方法。这样对我有用。

I don't know wehere do you try to add the buttons on the UIToolbar but you should try to add them in -(void)viewDidAppear method of your UIViewController class. In this way it worked for me.

这篇关于子类化UIToolbar并覆盖drawRect:-不显示UIBarButtonItems的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆