2015-05-12 58 views
3

我一直想通过1)IB UIToolbarUILabel用于显示文本,以及2)以编程方式通过UIToolbar(frame: CGRectMake(x,y,width,height))在“Send To ...”页面底部复制Snapchat的蓝色弹出矩形提示。在界面生成器中,我无法在工具栏上拖动标签,而且我一直在努力编写程序化路线。如何将UILabel添加到UIToolbar?

在Swift中复制该工具栏格式的最佳方法是什么?我的视图控制器与Snapchat很相似。点击某人的姓名,工具栏从底部以动画形式出现,收件人的数组显示在工具栏上。

我会发布一个Snapchat示例的图片,但我是S.O的新手,并且还没有代表。 :/ 提前致谢!

编辑:这是我指的是一个链接。底部蓝色发送提示。它仅在选择1个或更多用户时出现。 screenshot

+0

分享您的预期输出的截图。 –

+0

谢谢@sasquatch。我甚至没有足够的声望发布截图。 – chicobermuda

+0

如果我的答案可以帮助您,请将其标记为已接受。 –

回答

-1

试试这个在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

使用它...

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