要运行示例项目,先克隆仓库,然后从示例目录运行pod install
CHGInputAccessoryView可以通过CocoaPods获得。要安装它,只需将以下行添加到您的Podfile即可
pod "CHGInputAccessoryView"
首先,您必须准备一个UIView或UIViewController使其成为第一个响应者,并附加一个输入访问视图。
使UIView/UIViewController的inputAccessoryView属性可写
@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;
确保您的UIView/UIViewController可以成为第一个响应者
- (BOOL)canBecomeFirstResponder
{
return YES;
}
设置新的CHGInputAccessoryView与设置UIToolbar一样简单。
// get an instance of CHGInputAccessoryView
CHGInputAccessoryView *accessoryView = [CHGInputAccessoryView inputAccessoryView];
// optionally define a delegate
accessoryView.inputAccessoryViewDelegate = self;
// create a CHGInputAccessoryViewItem
CHGInputAccessoryViewItem *trashItem = [CHGInputAccessoryViewItem buttonWithTitle:@"Trash"];
// attach accessory view items
accessoryView.items = @[ trashItem ];
// attach the accessory view
self.inputAccessoryView = accessoryView;
CHGInputAccessoryView附带一些预构建项目
[CHGInputAccessoryViewItem buttonWithTitle:@"my title"];
[CHGInputAccessoryViewItem buttonWithImage:[UIImage imageNamed:@"my_image"]];
[CHGInputAccessoryViewItem separatorWithColor:[UIColor lightGrayColor] height:20.f]
CHGInputAccessoryViewItemTextField *item = [CHGInputAccessoryViewItemTextField item];
item.textField.placeholder = @"Enter your text";
CHGInputAccessoryViewItemTextField *item = [CHGInputAccessoryViewItemTextField item];
item.textField.placeholder = @"Enter your text";
提示:通过访问CHGInputAccessoryViewItemTextField/TextView属性找到textField/textView以修改textField/textView。 textField/textView将自动调整到最大可用宽度。 textView也会自动调整到最大可用高度。如果您想为textField/textView指定固定大小,请为其分配一个框架并设置CHGInputAccessoryViewItem属性flexibleSize为NO。
CHGInputAccessoryViewItemSwitch *item = [CHGInputAccessoryViewItemSwitch item];
item.switchView.on = YES;
CHGInputAccessoryView *accessoryView = [CHGInputAccessoryView inputAccessoryView];
accessoryView.progressView.progress = 0.5f;
[CHGInputAccessoryViewItemTextField flexibleSpace];
[CHGInputAccessoryViewItemTextField fixedSpace:25.f];
// enable item
[accessoryView enableItem:item];
// disable item
[accessoryView disableItemAtIndex:1];
要使用内置的代理(didTapItem:和didTapItemAtIndex:),请分配一个CHGInputAccessoryViewDelegate。
accessoryView.inputAccessoryViewDelegate = self;
item.target = self;
item.action = @selector(didTapMyItem:);
通过设置目标,内置的代理将不会为此项目调用。
item.actionOnTap = ^(CHGInputAccessoryViewItem *item){
NSLog(@"Tapped my item...");
};
- (void)setItems:(NSArray<CHGInputAccessoryViewItem *> *)items;
- (void)setItems:(NSArray<CHGInputAccessoryViewItem *> *)items animated:(BOOL)animated;
- (void)addItem:(CHGInputAccessoryViewItem *)item animated:(BOOL)animated;
- (void)addItem:(CHGInputAccessoryViewItem *)item atIndex:(NSUInteger)index animated:(BOOL)animated;
- (void)removeItem:(CHGInputAccessoryViewItem *)item animated:(BOOL)animated;;
- (void)removeItemAtIndex:(NSUInteger)index animated:(BOOL)animated;
克里斯蒂安·格雷特,[email protected]
CHGInputAccessoryView 以 MIT 许可协议提供。更多信息请参阅 LICENSE 文件。