Adding a custom label in toolbar doesn't work

前端 未结 2 1963

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

2条回答
  •  感动是毒
    2021-01-14 06:54

    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

提交回复
热议问题