正确的UIToolbar子类化方法

时间:2011-09-15 16:19:55

标签: iphone objective-c uinavigationcontroller uitoolbar

我第一次将UIToolbar子类化为使用自定义UIBarButton创建的。

我这样做:

@interface CustomToolbar : UIToolbar

@end


@implementation CustomToolbar

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // add buttons
        UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Button" style:UIBarButtonItemStyleBordered target:self action:@selector(pressSignupButton:)];

        // add buttons to the array
        NSArray *items = [NSArray arrayWithObjects:myButton, nil];

        [self setItems:items];
    }
    return self;
}

@end

然后在我的视图控制器中:

CustomToolbar *myToolbar = [[CustomToolbar alloc] init];
[self.navigationController.view addSubview:myToolbar];

问题是我可以看到工具栏但没有按钮。为什么呢?

注意:我更喜欢在没有笔尖的情况下以编程方式进行所有操作。

1 个答案:

答案 0 :(得分:1)

这有用吗?

CustomToolbar *myToolbar = [[CustomToolbar alloc] 
     initWithFrame:CGRectMake(0,0,self.navigationController.view.frame.size.width, 44)];
[self.navigationController.view addSubview:myToolbar];