轻松将附件输入视图添加到自定义输入工具栏。
通常在应用的消息页面或评论页面,您需要在视图的底部附加一个输入工具栏。这个输入工具栏将包含一个文本框和一个按钮。有时您会发现写聊天信息或评论不够用,您可能还想要发送贴纸、支付、联系人信息、位置信息等。您最简单的做法是在输入工具栏中添加额外的按钮并将其连接到动作表。使用这个类别,您不仅可以做到这些,还可以设计一个输入视图,并告诉ViewController您想要的输入视图。
YTPAccessoryInputView 需要 iOS 10.0 或更高版本。
假设您已经连接了输入工具栏、输入工具栏按钮和底部的布局约束。
@property (weak, nonatomic) IBOutlet UIView *chatInputToolBar; // Your tool bar
@property (weak, nonatomic) IBOutlet UIButton *chatInputToolBarButton; // Button in your tool bar to bring accessory input view up
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomConstraint; // Constraint from input tool bar to the bottom of the view
现在告诉您的ViewController您想使用的自定义输入视图。
UIView *inputView = [[[NSBundle mainBundle] loadNibNamed:@"YourInputView" owner:self options:nil] firstObject];
self.accessoryInputView = inputView;
self.inputToolBar = self.chatInputToolBar;
self.inputToolBarBottomSpace = self.bottomConstraint;
然后使用以下调用配置您的输入视图:
[self ytp_configureAccessoryInputView];
下一步是将按钮连接到触发您的自定义输入视图。在您的按钮的IBAction中添加以下内容:
[self ytp_toggleAccessoryInputViewWithButton:self.chatInputToolBarButton];
如果您想要消失键盘或您的自定义附件输入视图,例如
[self ytp_dismissKeyboardOrAccessoryInputView];
最后,您需要调用清理方法来消失键盘观察者。理想的放置位置是在viewDidDisappear中。
[self ytp_resetViewControllerStatus];
就这样!!
carlpan, [email protected]
YTPAccessoryInputView 受MIT许可证的约束。查看LICENSE文件获取更多信息。