UITextView hidekeyboard
I am trying to send message with photo to evernote. I created view for this
first I put UITextField
for Title
Then I put UITextView
for message and given a button to attach photo and have one image view to hold image from UIImagePicker
. When I write some text in UITextView
keyboard will c开发者_StackOverflowover attach photo button and image button. How to hide keyboard...or can I minimize height of keyboard
No need to minimize the height you can slide up the view and and after enter the text you need to hide the keyboard so for this,
Hide keyboard- Either add a tool bar above the keyboard or add a bar button in nav bar. and on click of the button
[youTextView resignFirstResponder];
for add tool bar make tool bar below the view make and when tap the text View a delegate will call textViewShouldBeganEditing
in this write some thing like this
CGRect tlFrame=yourToolBar.frame;
tlFrame.origin.y-=40; //check this according to you
yourToolBar.frame=tlFrame.origin.y;
also add some animation and , make its origin.y as it is when hide the keyboard.
Keep a background button and when user is done with typing text view message, you can expect the user to tap somewhere outside so that this background button takes the tap event and dismisses the keyboard.
Something as follows:
-(IBAction) backgroundButtonPressed:(id) sender {
[self.textView resignFirstResponder];
}
If you hide the keyboard then how do you expect the user to input the message? And you can customize the keyboard to change its height. One ideal way to do it is to move your view up so that when the keyboard comes, the button and image are still visible.
Well for this you can animate the view as..
CGFloat animatedDistance; // in.h file
and in .m file do the following
#pragma mark (Text View Delegate Method)
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"])
{
[textView resignFirstResponder];
return NO;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;
CGRect textFieldRect;
CGRect viewRect;
textFieldRect =[self.view.window convertRect:textView.bounds fromView:textView];
viewRect =[self.view.window convertRect:self.view.bounds fromView:self.view];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 1.0)
{
heightFraction = 1.0;
}
UIInterfaceOrientation orientation =[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait ||orientation == UIInterfaceOrientationPortraitUpsideDown)
{
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
}
else
{
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame;
viewFrame= self.view.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
if(textView.tag==0)
{
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
CGRect viewFrame;
viewFrame= self.view.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
}
This will solve your problem,so that nothing would be hidden there
Call this method in done button
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
精彩评论