2013-07-22 47 views
4

我對iOS中的一項功能感到好奇。如果可以的話,請幫助我。iOS:如何調整iOS中鍵盤覆蓋的區域高度

場景:我正在使用輸入名稱的文本框。它在屏幕的下半部分。在文本框下面是一個標籤,顯示剩餘字符的數量(例如,像在Twitter中)。

問題:當我把文本框放在屏幕的上半部分。文本字段和標籤都可見。但是當我將它們放在下半部分時,蘋果鍵盤會覆蓋標籤部分。 有沒有一種方法可以控制覆蓋區域,使下面的標籤也可見? 我希望我已經說清楚了。 謝謝。

+0

http:// stackoverflow。com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present –

+0

http://blog.avabodh.com/2012/03/making-ios-text-fieldview- visible-which.html –

+0

http://stackoverflow.com/questions/1775860/uitextfield-move-view-when-keyboard-appears?rq=1 –

回答

5

在這裏,我已經使用的委託方法的UITextView同樣的方式,你可以在這個代碼UITexField做

,尤其是圓形,當用戶開始在TextView的輸入值,它使您的視圖的HIGHT較小則其原創動畫

- 當用戶結束輸入值時,它會使您的視圖的大小爲原始大小。

-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 

    CGRect frame = self.view.frame; 
    frame.origin.y = -100; 
    [self.view setFrame:frame]; 
    [UIView commitAnimations]; 
} 
-(void)textViewDidEndEditing:(UITextView *)textView 
{ 
    CGRect frame = self.view.frame; 
    frame.origin.y = 0; 
    [self.view setFrame:frame]; 
    [UIView commitAnimations]; 

} 

如果你想了解這個委託幫助link

2

那麼在這種情況下,您必須在彈出鍵盤時移動文本框。您可以註冊通知以知道何時彈出鍵盤和滾動視圖以將屏幕上的所有內容向上滾動以便爲您完成工作

看到這個question,這很好地解釋瞭如何管理這樣的事情

2

AFAIK你無法控制iOS原生鍵盤的大小,都可以和應該做的是,使之成爲一個滾動的subivew查看並向上滾動。

所以通常的做法是這樣的。

  • 訂閱鍵盤通知。 UIKeyboardWillShowNotification
  • 在通知偵聽器將調用的方法中,相應地設置scrollView的內容大小並設置內容偏移量。

    self.scrollView.contentSize = CGSizeMake(320, 267); 
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, localKeyboardFrame.size.height, 0); 
    [self.scrollView scrollRectToVisible:<rect of view you want to scroll to> animated:YES]; 
    
  • 藉助適當的通知撤消鍵盤隱藏時的更改。 UIKeyboardWillHideNotification

    self.scrollView.contentInset = UIEdgeInsetsZero 
    

這裏是iOS Human Interface Guide's explanation就可以了。

+0

滾動不是解決方案,因爲所顯示的標籤應該由用戶同時查看同時在文本框中輸入值。 – na19

+0

使textview和你的用戶界面標籤在一個視圖和子視圖它滾動查看 –

+0

改變視圖元素的位置,根據其他答案是一個解決方案,但改變你的框架位置這樣的東西不是最佳的既不推薦。 –

1

添加下列內容viewDidLoad方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showKeyboard) name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideKeyboard) name:UIKeyboardWillHideNotification object:nil]; 

而經過that--中聲明你的.m文件下列2種方法

-(void)showKeyboard { 
    [UIView beginAnimations:@"slide" context:nil]; 
    [UIView setAnimationDuration:0.3]; 
    self.view.transform = CGAffineTransformMakeTranslation(0, -100); 
    [UIView commitAnimations]; } 

-(void)hideKeyboard { 
    [UIView beginAnimations:@"slide" context:nil]; 
    [UIView setAnimationDuration:0.1]; 
    self.view.transform = CGAffineTransformMakeTranslation(0, 0); 
    [UIView commitAnimations]; }