TURecipientBar
是一个 UIView
,用于输入收件人,类似于 iOS 邮件应用。它处理收件人的搜索和显示。
安装
CocoaPods
- 在您的 Podfile 中添加
pod TURecipientBar, '~> 2.0'
- 运行 'pod install'
- 导入
TURecipientsBar.h
和TURecipientsDisplayController.h
手动
- 将
TURecipientBar
子文件夹复制到您的项目中。 - 唯一需要的框架是 UIKit 和 iOS 6.0。
- 导入
TURecipientsBar.h
和TURecipientsDisplayController.h
使用
请参阅包含的示例项目以及文档。
TURecipientBar
使用自动布局。这意味着您需要添加约束来定位它。它会自动更新其高度约束,如果您的约束设置正确,所有内容都会根据需要进行调整。
通常这包括为栏的顶部、左侧、右侧和底部添加约束。底部约束通常是与另一个视图的垂直间距。
如果您正在使用 Interface Builder 布局视图,您将被迫添加约束以使布局非歧义。在这种情况下,您可以添加一个低于 1000 的优先级(因此不是必需的)的高度约束并连接到 在 Xcode 5 中,您现在可以指定在构建时将删除的占位符约束。或者,您可以完全不添加,但这将生成警告。heightConstraint
IBOutlet。
对于搜索,您需要创建一个 TURecipientsDisplayController
实例。此类处理搜索和显示搜索结果的逻辑。然后您实现一个 UITableViewDataSource
来显示搜索结果并重写 composeDisplayController:shouldReloadTableForSearchString:
以更新搜索结果。
您需要连接以下 TURecipientsDisplayController
的输出:
recipientsBar
:TURecipientsBar
。contentsController
:搜索结果将被放置的视图控制器。几乎所有情况下,都是recipientsBar
所在的视图控制器。searchResultsDataSource
:搜索表格视图的数据源。searchResultsDelegate
:搜索表格视图的代理。通常与searchResultsDataSource
相同。recipientsBar.delegate
:将recipientsBar
的代理连接到TURecipientsDisplayController
。
您还需要保持对 TURecipientsDisplayController
的引用,否则它将被释放。
对于 iOS 7,您需要将该栏视图控制器设置为 automaticallyAdjustsScrollViewInsets = NO
。如果不这样做,它将导致受收件人栏(它是 UIScrollView
的子类)的行为异常。我正在调查这些问题以删除这个要求,这可能实际上是一个 iOS 7 的bug。
定制
隐藏添加按钮
self.recipientsBar.showsAddButton = NO;
添加占位文本
self.recipientsBar.placeholder = NSLocalizedString(@"Type names...", nil);
修改标签文本
self.recipientsBar.label = @"Invite:";
禁用搜索表格视图
- (BOOL)recipientsDisplayControllerShouldBeginSearch:(TURecipientDisplayController *)controller
{
return NO;
}
更改收件人外观
UIImage *backgroundImage = [[UIImage imageNamed:@"token"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0];
[[TURecipientsBar appearance] setRecipientBackgroundImage:backgroundImage forState:UIControlStateNormal];
NSDictionary *attributes = @{
NSFontAttributeName: [UIFont fontWithName:@"American Typewriter" size:14.0],
NSForegroundColorAttributeName: [UIColor yellowColor],
};
[[TURecipientsBar appearance] setRecipientTitleTextAttributes:attributes forState:UIControlStateNormal];
更改标签外观
NSDictionary *labelAttributes = @{
NSFontAttributeName: [UIFont fontWithName:@"Marker Felt" size:14.0],
NSForegroundColorAttributeName: [UIColor redColor],
};
[[TURecipientsBar appearance] setLabelTextAttributes:labelAttributes];