2012-09-10 82 views
0

我有uiscrollview,2 textviewfields和我閱讀所有關於鍵盤隱藏textview的帖子,並嘗試了很多方法,但仍然隱藏它。UIScrollView和仍然鍵盤隱藏文字

我的代碼是這樣的: ViewController.h

@property(nonatomic,retain) IBOutlet UITextView *campaignTitle; 
@property (weak, nonatomic) IBOutlet UITextView *campaignDescription; 
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; 
@property UITextView *activeTextView; 

ViewController.m

@synthesize campaignTitle; 
@synthesize campaignDescription; 
@synthesize scrollView; 
@synthesize activeTextView; 

#define SCROLLVIEW_CONTENT_HEIGHT 460 
#define SCROLLVIEW_CONTENT_WIDTH 320 

- (void)viewDidLoad 
{ 
[[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification 
               object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillHide:) 
               name:UIKeyboardWillHideNotification 
               object:nil]; 
    scrollView.contentSize=CGSizeMake(0, self.view.frame.origin.y+self.view.frame.size.height+50); 
    [super viewDidLoad]; 
} 

- (BOOL) textViewShouldBeginEditing:(UITextView *)textView 
{ 
    textView.text = @""; 
    self.activeTextView = textView; 
    return YES; 
} 

- (BOOL) textViewShouldEndEditing:(UITextView *)textView 
{ 
    self.activeTextView = nil; 
    return YES; 
} 

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    if([text isEqualToString:@"\n"]) 
     [textView resignFirstResponder]; 
    return YES; 
} 

- (void)keyboardWasShown:(NSNotification *)notification 
{ 
    // Step 1: Get the size of the keyboard. 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height. 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
    // Step 3: Scroll the target text field into view. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= keyboardSize.height; 
    if (!CGRectContainsPoint(aRect, activeTextView.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, activeTextView.frame.origin.y - (keyboardSize.height-15)); 
     [scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

- (void) keyboardWillHide:(NSNotification *)notification { 

    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 

- (IBAction)dismissKeyboard:(id)sender 
{ 
    [self.activeTextView resignFirstResponder]; 
} 

有從滾動視圖和campaignTitle,campaignDescription代表團。

出了什麼問題?爲什麼鍵盤仍然隱藏底部uitextview?您可能會看到的額外代碼是支持UITextView中的Return按鈕

回答

1

確保您的UITextFields實際上位於故事板內的UIScrollView的內部,並且不會意外放置在它下面的UIView中。

setContentOffset應該做SOMETHING,即使它不是預期的效果。由於它看起來什麼都沒做,我懷疑你的UITextFields不是它的子視圖,或者你的UIScrollView沒有在界面構建器中連接。

2

或使用TPKeyboardAvoiding。 (通過身份檢查在廈門國際銀行仍然 )

  1. 添加一個UIScrollView到您的視圖控制器的XIB
  2. 的滾動視圖類爲TPKeyboardAvoidingScrollView
  3. : 這是偉大的工作,而且非常易於安裝
  4. 將所有的滾動型

它還可以自動掛鉤鍵盤上了「下一步」按鈕,在文本字段內切換的控件。

+0

感謝您的支持!看起來有些落後,iOS本身並不關心這一點。 – Merkidemis