Adding a custom label in toolbar doesn't work

2020-03-26 07:18发布

问题:

I'm trying to add a custom label in my UINavigationController's toolbar. I followed the top answer for this question but it doesn't seem to work for me and I don't know why. The custom text doesn't appear, but the button is there. It highlights when I press it but it has no text.

Here's my code:

- (void) editToolbar{
    NSArray *toolbarItemsCopy = [self.toolbarItems copy];

    //set up the label
    self.notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width/0.25, 21.0f)];
    self.notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:20];
    self.notificationsLabel.backgroundColor = [UIColor clearColor];
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
    self.notificationsLabel.textColor = [UIColor blueColor];
    self.notificationsLabel.text = @"Checking server";
    self.notificationsLabel.textAlignment = UITextAlignmentCenter;

    //init a button with custom view using the label
    UIBarButtonItem *notif = [[UIBarButtonItem alloc] initWithCustomView:self.notificationsLabel];

    //add to the toolbarItems
    NSMutableArray *toolbarItemsMutableArray = [[NSMutableArray alloc]init];

    NSLog(@"toolbar %i", self.toolbarItems.count);

    //have to add the custom bar button item in a certain place in the toolbar, it should be the 3rd item
    for (int i = 0; i < toolbarItemsCopy.count; i++) {
        if (i == 2){                
            [toolbarItemsMutableArray addObject:notif];
        }
        NSLog(@"toolbar item %i %@", i,[toolbarItemsCopy objectAtIndex:i]);
        [toolbarItemsMutableArray addObject:[toolbarItemsCopy objectAtIndex:i]];
    }
    if (toolbarItemsCopy.count == 4){
    }else{
        //remove the previous custom label
        [toolbarItemsMutableArray removeObjectAtIndex:3];
    }
    self.toolbarItems = toolbarItemsMutableArray;
    //[self.navigationController.toolbar setItems: toolbarItemsMutableArray animated:YES];

    NSLog(@"toolbar %i", self.toolbarItems.count);
}https://stackoverflow.com/questions/ask

This is what NSLog is printing:

Catalogue[361:10403] toolbar 4
Catalogue[361:10403] toolbar item 0 <UIBarButtonItem: 0x8a24650>
Catalogue[361:10403] toolbar item 1 <UIBarButtonItem: 0x8a36cd0>
Catalogue[361:10403] toolbar item 2 <UIBarButtonItem: 0x8a36d30>
Catalogue[361:10403] toolbar item 3 <UIBarButtonItem: 0x8a382f0>
Catalogue[361:10403] toolbar 5

This is how I solved it:

I have to alloc and init a UILabel inside the function, instead of using an ivar.

UILabel *notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
    notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    notificationsLabel.backgroundColor = [UIColor clearColor];
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
    notificationsLabel.textColor = [UIColor whiteColor];
    notificationsLabel.text = @"Checking server";
    notificationsLabel.textAlignment = UITextAlignmentCenter;

回答1:

I am sure that this code will help you. Make changes in your code while keeping this code in mind. I have worked on this and it is working fine,

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolBar];

UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f,     self.view.frame.size.width, 21.0f)];
[titleLbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTextColor=[UIColor blueColor];
[titleLbl setText:@"Title"];
[titleLbl setTextAlignment:UITextAlignmentCenter];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:titleLbl];
NSArray *toolbarItemArr = [NSArray arrayWithObjects:button, nil];
[toolBar setItems:toolbarItemArr animated:YES];

For better understanding you can follow these links: UIToolBar Class Reference and UIBarButtonItem Class Reference



回答2:

I know this question is answered and old, but I had the same experience, and found something interesting. My code needs to display (in the toolbar) a button sometimes, and a label some others, depending on the state of the program. If I create a UILabel object in my controller class, and reuse it in calls to

UIBarButtonItem *myItem = [[UIBarButtonItem alloc] initWithCustomView: _myUILabel];
[self setToolbarItems: [NSArray arrayWithObject: myItem]];
[myItem release];

every time I need to switch from the button to the label, then the label will not show up (Actually it shows up the first time, and after switching back and forth once, it stops showing up). I spent hours on the issue, and if it's not for the fact I'm bald, I would probably pulled all my hair out in despair anyway. I verified all my retain counts are fine. My saved label is good and valid.

I found that if I create a new label every time I need to show it in the toolbar, then the problem goes away. There is definitely an issue in the handling of a UILabel inside setToolbarItems. So, my advice, create a new label, set the text, etc., wrap it in a UIToolBarItem, and set your toolbar items again.

It's not ideal, it exercises alloc and release more, but it works. I hope this will help someone out there.