2013-02-01 27 views
-2

可能重複數據:
Making the view slide up to make room for the keyboard?
Xcode/iOS5: Move UIView up, when keyboard appears如何在iphone應用滑動屏幕看起來我們正在進入文本框

正如我們通常輸入數據中顯示文本框的鍵盤並且它隱藏了我們在字段中輸入的數據,所以有什麼方法可以使屏幕滑動,以便我們可以看到輸入到fiedl中的數據。

+0

可能重複:http://stackoverflow.com/questions/7952762/xcode-ios5-move-uiview-up-when-keyboard-appears,http://stackoverflow.com/questions/5893122/slide-form-上時,鍵盤會出現 – iLearner

回答

1

試試這個代碼.......

-(void)setViewMovedUp:(BOOL)movedUp 
{ 
[UIView beginAnimations:nil context:NULL]; 
[UIView setAnimationDuration:0.3]; // if you want to slide up the view 

CGRect rect = self.view.frame; 
if (movedUp) 
{ 
    rect.origin.y -= moveKeyboard; 
} 
else 
{ 
    rect.origin.y += moveKeyboard; 
} 
self.view.frame = rect; 
[UIView commitAnimations]; 
} 

    -(void)keyboardWillShow 
{ 
// Animate the current view out of the way 
if (self.view.frame.origin.y >= 0) 
{ 
    [self setViewMovedUp:YES]; 
} 
else if (self.view.frame.origin.y < 0) 
{ 
    [self setViewMovedUp:NO]; 
} 
} 


-(void)keyboardWillHide 
{ 
if (self.view.frame.origin.y >= 0) 
{ 
    [self setViewMovedUp:YES]; 
} 
else if (self.view.frame.origin.y < 0) 
{ 
    [self setViewMovedUp:NO]; 
} 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
[super viewWillAppear:animated]; 
// register for keyboard notifications 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) 
              name:UIKeyboardWillHideNotification object:nil]; 
} 
- (void)viewWillDisappear:(BOOL)animated 
{ 
[super viewWillDisappear:animated]; 
// unregister for keyboard notifications while not visible. 
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 

編輯:moveKeyboard是浮動。根據您的需要設定其值。

0

有通知(UIKeyboard[Will|Did][Show|Hide]Notification)告訴您鍵盤即將出現或消失,並且您可以使用這些通知來觸發移動視圖的代碼。移動視圖有不同的方式 - 您可以自己移動它們,根據需要調整其位置;你可以把它們放在一個視圖中,這樣你只需要移動容器;或者您可以將它們嵌入到滾動視圖中,並簡單地調整滾動視圖的內容偏移量。

參見Apple的文件Managing the Keyboard,特別是名爲Moving Content That is Located Under the Keyboard的部分。這裏也有示例代碼,它工作得很好。

相關問題