2016-02-14 88 views

回答

0

我試過解決方案,你的問題,我只是得到。

#define kOFFSET_FOR_KEYBOARD 80.0 
@interface ViewController() 
{ 
    NSInteger tagTF; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

-(void)setMovedUp:(BOOL)movedUP 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    CGRect rect = self.view.frame; 
    if(movedUP) 
    { 
    rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
    rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else{ 
    rect.origin.y += kOFFSET_FOR_KEYBOARD; 
    rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 
    [UIView commitAnimations]; 
} 

#pragma mark - UITextFieldDelegate Methods 

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    tagTF = textField.tag; 
    if(textField.tag>=6) 
    { 
    if(self.view.frame.origin.y >= 0) 
    { 
     [self setMovedUp:YES]; 
    } 
    } 
    return YES; 
} 


-(BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    if(textField.tag>=6) 
    { 
    [self setMovedUp:NO]; 
    } 
    [textField resignFirstResponder]; 
    return YES; 
} 

從我上面的回答我設置文本框的標籤從1到8.It完美的作品你want.Please設置文本框的委託。

+0

非常感謝你Ruchira蘭達納它爲我工作。 – user5575941

0

我有一個廣泛的博客文章,解釋如何做到這一點:

Shifting views to make room for the keyboard

它的要點是添加通知處理程序鍵盤將顯示和鍵盤會隱藏通知。在這些處理程序中,您可以獲得鍵盤的高度,然後進行一些數學計算,以確定您需要將該字段向上移動,以便鍵盤無法覆蓋它。然後,您將視圖控制器的視圖移到足以保持視野暴露的位置。

它變得複雜多了,但是這涵蓋的所有博客文章(這是a larger blog post on animating random shapes包括鍵盤技術的工作示例部分。)

0

有很多該選項。

我長時間在使用TPKeyboardAvoidingScrollView。現在,我使用IQKeyboardManager更簡單,更易於集成。

IQKeyboardManager

0

here是由蘋果公司關於處理鍵盤的官方文檔。

0
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField { 
CGRect frame = self.view.frame; 
Float y = frame.origin.y - 70; 
frame.origin.y = y; 
self.view.frame = frame; 
return YES; 
} 

- (void) textFieldDidEndEditing:(UITextField *)textField{ 
CGRect frame = self.view.frame; 
Float y = frame.origin.y + 70; 
frame.origin.y = y; 
self.view.frame = frame; 
} 

如果有變化的y值(如70,80,......)在textFieldShouldBeginEditing方法,並增加相同的值,在textFieldDidEndEditing方法Y。

試試這個代碼。

相關問題