0

我正在使用一個包含滾動視圖的登錄屏幕,並且在滾動視圖中有兩個帶有登錄按鈕的文本字段。鍵盤隱藏在輸入文本到文本框之間

scrollview用於調整iphone 5的屏幕尺寸。而我正在使用「標籤手勢」,因此如果任何用戶在文本字段中輸入文本並且想要隱藏鍵盤,則可以點擊屏幕上的任何位置以隱藏鍵盤。用於標籤手勢功能是

- (void)viewDidLoad { 

NSLog(@"login view"); 

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; 
[self.scrollView addGestureRecognizer:singleTap]; } 

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture 
{ 
[self.view endEditing:YES]; } 

我的問題是,當用戶在文本框用鍵盤在輸入文本的鍵盤的中間輸入文本然後檢測的標籤手勢並隱藏在中間的鍵盤。

我做了什麼來解決這個問題: - 1)我改變了[self.view addGestureRecognizer:singleTap]。

2.)我用尺寸(0,0,360,400)在屏幕的頂部放置了一個視圖,並將該手勢應用於該視圖,以便點擊該視圖將隱藏鍵盤,但仍然在用戶通過調用手勢鍵入鍵盤時隱藏方法

3)我還使用屏幕大小的一半滾動型按鈕,以便taht用戶可以點擊任何地方隱藏keybaord但坎打字時甚至然後揣鍵盤調用Ÿ按鈕的IBAction爲方法的地點理想,

回答

2

用途:隱藏鍵盤並顯示鍵盤: -

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField == username) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; 

    } 
    if (textField == password) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil]; 

    } 

    return YES; 
} 

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{ 
    if (textField == username) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil]; 
    } 

    if (textField == password) 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidShowNotification object:nil]; 

    } 
    return YES; 
} 


- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    float newVerticalPosition = -keyboardSize.height + 100; 

    [self moveFrameToVerticalPosition:newVerticalPosition forDuration:0.3f]; 
} 


- (void)keyboardWillHide:(NSNotification *)notification 
{ 
    CGFloat kNavBarHeight = self.navigationController.navigationBar.frame.size.height; 
    [self moveFrameToVerticalPosition:kNavBarHeight forDuration:0.3f]; 
} 

- (void)moveFrameToVerticalPosition:(float)position forDuration:(float)duration 
{ 
    CGRect frame = self.view.frame; 
    frame.origin.y = position; 

    [UIView animateWithDuration:duration animations:^{ 
     self.view.frame = frame; 
    }]; 
} 
+0

感謝您的迴應...我已經實現了您提供的相同代碼...但它沒有解決我的問題...我的問題在於,當鍵入鍵盤時通過調用手勢方法自動隱藏。 –

+0

評論,tapgesture方法只有這個方法,我已經張貼在這裏使用在你的項目中它會幫助你... – 2016-04-25 12:57:55

+0

好吧...謝謝我會評論標籤手勢....但之後,點擊視圖不會解僱鍵盤...我猜 ? –

3

刪除Tapgesture並試試這段代碼,它會有所幫助。

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 

    [self.view endEditing:YES]; 
} 
+0

我實現了它正在工作的方法,但在返回 –

+0

後,它會在頂部給出黑色空間,檢查您爲輕擊手勢放置的視圖並檢查滾動視圖。 –

+0

與scrollview該方法沒有得到調用... –

1

嘗試這樣,

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ 

[textField1 resignFirstResponder]; 
[textField2 resignFirstResponder]; 
} 

希望這將有助於:)

+0

感謝您的迴應....實際上我使用視圖上的滾動視圖....所以觸摸方法將無法正常工作,直到我刪除滾動視圖和觸摸只被檢測到在自己的看法.... –

+0

然後添加該滾動視圖上的視圖,然後將文本框添加到該視圖 – Lion

+0

因爲我使整個功能自定義像根據屏幕大小移動文本字段與滾動視圖...我只是刪除選項卡手勢和scrollview ......我用@aayush ans來自動調整鍵盤出現時的視圖,並使用touchesBegan方法來處理觸摸並隱藏鍵盤....再次感謝 –

2

我爲它找到最佳的解決方案。首先,您將Pod「TPKeyboardAvoiding」,'〜> 1.2.3'集成在一起,然後添加TPKeyboardAvoidingScrollView類。它將處理所有數據。不必編寫額外的代碼。

+0

感謝您的迴應....我將從現在起使用相同的 –