To run the example project, clone the repo, and run pod install
from the Example directory first.
CHGInputAccessoryView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "CHGInputAccessoryView"
At first you must prepare a UIView or a UIViewController to become a first responder and attach an input accessory view.
Make UIView/UIViewController property inputAccessoryView writeable:
@property (nonatomic, readwrite, retain) UIView *inputAccessoryView;
Make sure your UIView/UIViewController can become first responder:
- (BOOL)canBecomeFirstResponder
{
return YES;
}
Setting up a new CHGInputAccessoryView is as simple as setting up a 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 comes with some prebuild items:
- a button with title
[CHGInputAccessoryViewItem buttonWithTitle:@"my title"];
- a button with image
[CHGInputAccessoryViewItem buttonWithImage:[UIImage imageNamed:@"my_image"]];
- a separator
[CHGInputAccessoryViewItem separatorWithColor:[UIColor lightGrayColor] height:20.f]
- a UITextField
CHGInputAccessoryViewItemTextField *item = [CHGInputAccessoryViewItemTextField item];
item.textField.placeholder = @"Enter your text";
- a UITextView
CHGInputAccessoryViewItemTextField *item = [CHGInputAccessoryViewItemTextField item];
item.textField.placeholder = @"Enter your text";
Tip: Modify the textField or textView by accessing CHGInputAccessoryViewItemTextField/TextView property textField/textView. The textField/textView will automatically resize to the maximum availabe width. The textView also resizes automatically to maximum available height. If you want a fixed size for your textField/textView assign a frame on it and set the CHGInputAccessoryViewItem property flexibleSize to NO.
- a UISwith
CHGInputAccessoryViewItemSwitch *item = [CHGInputAccessoryViewItemSwitch item];
item.switchView.on = YES;
- a UIProgressView The InputAccessoryView has a build-in UIProgressView.
CHGInputAccessoryView *accessoryView = [CHGInputAccessoryView inputAccessoryView];
accessoryView.progressView.progress = 0.5f;
- a flexible or fixed space
[CHGInputAccessoryViewItemTextField flexibleSpace];
[CHGInputAccessoryViewItemTextField fixedSpace:25.f];
// enable item
[accessoryView enableItem:item];
// disable item
[accessoryView disableItemAtIndex:1];
- InputAccssoryViewDelegate
To use the build-in delegates (didTapItem: and didTapItemAtIndex:) assign a CHGInputAccessoryViewDelegate.
accessoryView.inputAccessoryViewDelegate = self;
- Assign target and action
item.target = self;
item.action = @selector(didTapMyItem:);
By setting the target the build in delegates will not be called for this item.
- Assign action block
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;
Christian Greth, [email protected]
CHGInputAccessoryView is available under the MIT license. See the LICENSE file for more info.