我想用照片發送消息給evernote。我創建的視圖此UITextView hidekeyboard
首先,我把UITextField
爲標題 後來我把UITextView
的消息,並給出一個按鈕來添加照片,並有一個圖像視圖從UIImagePicker
持有圖像。當我在UITextView
中寫入一些文字時,鍵盤將會覆蓋附加照片按鈕和圖像按鈕。如何隱藏鍵盤......或者我可以最小化鍵盤的高度
我想用照片發送消息給evernote。我創建的視圖此UITextView hidekeyboard
首先,我把UITextField
爲標題 後來我把UITextView
的消息,並給出一個按鈕來添加照片,並有一個圖像視圖從UIImagePicker
持有圖像。當我在UITextView
中寫入一些文字時,鍵盤將會覆蓋附加照片按鈕和圖像按鈕。如何隱藏鍵盤......或者我可以最小化鍵盤的高度
保持一個背景按鈕,當用戶輸入文本視圖消息完成後,您可以期望用戶點擊外面的某個地方,以便此背景按鈕可以接受點按事件並關閉鍵盤。
東西如下:
-(IBAction) backgroundButtonPressed:(id) sender {
[self.textView resignFirstResponder];
}
如果你隱藏鍵盤,那麼你如何期望用戶輸入消息?而且您可以自定義鍵盤以更改其高度。一個理想的方法就是將視圖向上移動,這樣當鍵盤到來時,按鈕和圖像仍然可見。
沒有必要儘量減少你可以向上滑動的觀點和後輸入你需要隱藏鍵盤的文字高度,對於這一點,
隱藏鍵盤 - 在鍵盤上添加工具欄或在導航欄中添加欄按鈕。和按鈕
[youTextView resignFirstResponder];
的點擊視圖下方添加工具欄make工具欄做,當點擊文本查看委託將調用textViewShouldBeganEditing
在此寫一些這樣的事
CGRect tlFrame=yourToolBar.frame;
tlFrame.origin.y-=40; //check this according to you
yourToolBar.frame=tlFrame.origin.y;
也添加一些動畫,並在隱藏鍵盤時使其原來的原樣。
好吧我會嘗試這個,我從來沒有添加鍵盤上方的工具欄 – iProgrammer 2011-05-30 05:48:20
我用https://github.com/cwalcott/UIToolbar-Keyboard-Example來隱藏鍵盤與工具欄。但在這個例子中,當我點擊界面生成器時在工具欄上它不顯示。在我的代碼中,它總是在筆尖中可見。 – iProgrammer 2011-05-30 07:27:13
那麼這個可以動畫視圖..
CGFloat animatedDistance; // in.h file
和.m文件執行以下操作
#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];
}
}
這將解決您的問題,所以沒有什麼會被隱藏在那裏
調用此方法完成按鈕
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}
否我想在用戶完成寫入後隱藏鍵盤,然後去附加照片按鈕 – iProgrammer 2011-05-30 05:45:37
使用[yourTextField resignFirstResponder];完成輸入 – visakh7 2011-05-30 06:01:35