Fred Collins
Fred Collins

Reputation: 5010

Correct way to subclass UIToolbar

For the first time I'm subclassing UIToolbar for creating ones with custom UIBarButton.

I'm doing this:

@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

Then in my view controller:

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

The problem is that I can see the toolbar but there aren't buttons. Why?

NB: I prefer to have all programmatically without nib.

Upvotes: 2

Views: 2313

Answers (1)

Michael Frederick
Michael Frederick

Reputation: 16714

Does this work?

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

Upvotes: 1

Related Questions