2012-05-26 22 views
0

我有TextField1和TextField2
我想滾動滾動視圖只有當顯示鍵盤由於TextField2。 這是我的實際代碼。
有沒有解決方法?有條件的滾動視圖

-(void) viewWillAppear:(BOOL)animated {  
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardDidShow:) 
              name:UIKeyboardDidShowNotification 
              object:self.view.window]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardDidHide:) 
              name:UIKeyboardDidHideNotification 
              object:nil]; 
} 

-(void) keyboardDidShow:(NSNotification *) notification { 
    self.ScrollView.center = CGPointMake(self.originalCenter.x, 
             self.originalCenter.y-100); 
} 

-(void) keyboardDidHide:(NSNotification *) notification { 
    self.ScrollView.center = CGPointMake(self.originalCenter.x, 
             self.originalCenter.y); 
} 

回答

1

你需要監聽UITextfield委託方法:

textfield2.delegate = self; 

-(void)textFieldDidBeginEditing: (UITextField*)textField { 
    if (textField == textField2) { 
     //ENABLE THE SCROLLING 
    } 
} 

-(void)textFieldDidEndEditing: (UITextField*)textField { 
    if (textField == textField2) { 
     //DISABLE THE SCROLLING 
    } 
} 

只需定製方法您的需求。

如果需要是什麼時候的鍵盤顯示,你可以有一個布爾檢查:

if (textField == textField2) { 
    scrollBool = YES; 
    } 
} 

-(void)textFieldDidEndEditing: (UITextField*)textField { 
    if (textField == textField2) { 
     scrollBool = NO; 
    } 
} 

-(void)keyBoardDidShow.... { 
    if (scrollBool) { 
     // do the scrolling 
    } 
} 
+0

GREAT!非常感謝,用你的回答解決了,ciao! – Beppino66