How to add UILabel to UIToolbar?

I'm trying to echo a Snapchat request for a blue pop-up rectangle at the bottom of the Send to ... page through 1) IB UIToolbar

s UILabel

to display the text, and 2) programmatically through UIToolbar(frame: CGRectMake(x,y,width,height))

. In the interface builder, I am unable to drag the shortcut over the toolbar and I was struggling with the programmatic route.

What's the best way to replicate the formatting of this toolbar in Swift? My controller will be very similar to Snapchat's. Click on someone's name and the toolbar will animate below and an array of recipients will appear in the toolbar.

I'd post a photo of the Snapchat example, but I'm new to SO and don't have a reputation yet.: / Thanks in advance!

EDIT: Here's a link to what I'm talking about. The blue sending tip is at the very bottom. It only appears when 1 or more users are selected.screenshot

+3


source to share


1 answer


Try to show the shortcut on the toolbar.

NSMutableArray *items = [[self.toolbar items] mutableCopy];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer];


self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
[self.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[self.titleLabel setBackgroundColor:[UIColor clearColor]];
[self.titleLabel setTextColor:[UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0]];
[self.titleLabel setText:@"Title"];
[self.titleLabel setTextAlignment:NSTextAlignmentCenter];

UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:spacer2];


UIBarButtonItem *title = [[UIBarButtonItem alloc]    initWithCustomView:self.titleLabel];
[items addObject:title];

[self.toolbar setItems:items animated:YES];

      

OR



use it ...

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

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.backgroundColor = [UIColor clearColor];

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
toolbar.items = [NSArray arrayWithObject:item];

label.text = @"Hello World";

      

-1


source







All Articles