If you find yourself needing a
UITextView that sticks to the keyboard similar to an
inputAccessoryView that does not disappear when the keyboard hides, you'll quickly find that you'll likely have to build out a fairly time consuming custom view. MessageComposerView aims to save you all that setup time and headache and provide a simple, customizable implementation.
Rather that being an
inputAccessoryView, it is a custom
UIView that will automatically "stick" the keyboard, handling rotation, text changes and keyboard state changes.
####Setup In your header file:
- Import the
- Add the
- Optionally create a
MessageComposerViewproperty for your message composer view.
#import "MessageComposerView.h" @interface ViewController : UIViewController<MessageComposerViewDelegate> @property (nonatomic, strong) MessageComposerView *messageComposerView; @end
In your class file, instantiate and add
MessageComposerView to the bottom of your view controller. You can do this simply via
init (the default height is 54)
self.messageComposerView = [[MessageComposerView alloc] init]; self.messageComposerView.delegate = self; [self.view addSubview:self.messageComposerView];
There are several custom initializers that are also supported:
- (id)initWithKeyboardOffset:(NSInteger)offset andMaxHeight:(CGFloat)maxTVHeight;
init with screen width and default height. Offset provided is space between composer and keyboard/bottom of screen
- (id)initWithFrame:(CGRect)frame andKeyboardOffset:(NSInteger)offset;init with provided frame and offset between composer and keyboard/bottom of screen
- (id)initWithFrame:(CGRect)frame andKeyboardOffset:(NSInteger)offset andMaxHeight:(CGFloat)maxTVHeight;init with provided frame and offset between composer and keyboard/bottom of screen. Also set a max height on composer.
Message composerview supports text placeholders. If you want to customize the placeholder text, simply edit the
messagePlaceholder property like so:
self.messageComposerView.messagePlaceholder = @"Type a comment...";
If you want to add an accessory view (e.g. a camera button) you can do so via the
configureWithAccessory: function. This will automatically add your passed in
UIView to the left of the message text view. Configuration would go something like this:
UIButton *cameraButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; [self.cameraButton setImage:[UIImage cameraButtonImage] forState:UIControlStateNormal]; [self.cameraButton addTarget:self action:@selector(cameraClicked:) forControlEvents:UIControlEventTouchUpInside]; [self.cameraButton setContentMode:UIViewContentModeCenter]; [self.messageComposerView configureWithAccessory:cameraButton];
MessageComposerViewDelegate has several delegate methods:
Required - Triggered whenever the user presses the send button.
messageis the text within the
UITextViewat the time the button was pressed.
- (void)messageComposerFrameDidChange:(CGRect)frame withAnimationDuration:(CGFloat)duration andCurve:(NSInteger)curve;
Optional - Triggered whenever the UITextView frame is reconfigured.
frameis the CGRect that was applied to the MessageComposerView container. You can use this frame - namely the y pos - to determine the offset of your own views when the keyboard changes position. The duration will allow you to match the animation precisely.
Optional - Triggered whenever the UITextView text changes.
####CocoaPods If you're using (or want to use) the CocoaPods you can get the latest stable release by doing the following:
Install cocoapods and set it up (if you haven't already)
sudo gem install cocoapods
Create a textfile in your project's root directory called
Podfile(if you haven't already) and add the following line:
pod 'MessageComposerView', :git => 'https://github.com/oseparovic/MessageComposerView.git'
pod install. You should see something like the following:
Installing MessageComposerView (1.3.0)
Generating Pods project
Integrating client project
Start your project via the newly created
.xcworkspacefile. Cocoapods creates a seperate xcode project file that has the included pods set up for you. It should be in your project's root directory right alongside your
.xcodeprojfile if everything went smoothly!
How it works
MessageComposerView tries to avoid configuration in
UIKeyboardDidShowNotification as I found them to be at times excessive and difficult to properly manipulate, especially when it came to rotation.
UIKeyboardWillShowNotification notification is used solely to dynamically determine the keyboard animation duration on your device before any animations occur.
The actual resizing of the views is handled via
layoutSubviews and through the following