在工具栏中添加自定义标签不起作用 [英] Adding a custom label in toolbar doesn't work

查看:156
本文介绍了在工具栏中添加自定义标签不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UINavigationController的工具栏中添加自定义标签。我按照问题的最佳答案,但它似乎不起作用对我来说,我不知道为什么。自定义文本不会出现,但按钮就在那里。它突出显示我按它但它没有文字。

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.

这是我的代码:

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

这就是NSLog的用途打印:

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:

我必须在函数内部分配并初始化UILabel,而不是使用ivar。

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;


推荐答案

我相信此代码会对您有所帮助。在记住这些代码的同时对代码进行更改。我已经完成了这项工作并且工作正常,

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];

为了更好地理解,您可以关注以下链接:
UIToolBar类参考 UIBarButtonItem类参考

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

这篇关于在工具栏中添加自定义标签不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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