2016-12-29 38 views
-2

textView顯示或隱藏鍵盤時,我正在使用波紋管代碼向上或向下移動屏幕,通過這種方式,我可以在聚焦時看到最後一個字段。顯示鍵盤時不顯示導航欄

- (void)textViewDidBeginEditing:(UITextView *)textField 
{ 
    [self animateTextView: textField up: YES]; 
} 


- (void)textViewDidEndEditing:(UITextView *)textField 
{ 
    [self animateTextView: textField up: NO]; 
} 

- (void) animateTextView: (UITextView*) textField up: (BOOL) up 
{ 
    const int movementDistance = 130; // tweak as needed 
    const float movementDuration = 0.2f; // tweak as needed 

    int movement = (up ? -movementDistance : movementDistance); 

    [UIView beginAnimations: @"anim" context: nil]; 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimations]; 
} 

但問題是,navBar它也是移動時屏幕向上移動,在那裏所有的移動除了navBar或保持navBar在它的使用[self animateTextView: textField up: YES];後位置的方法嗎?

+0

爲什麼?你爲什麼要這樣設計你的觀點?爲什麼不使用'UINavigationController'?你爲什麼使用'beginAnimations:'API?這是什麼,iOS/iPhoneOS 3? –

回答

1

這是因爲您的主視圖中有您的自定義NavigationBar。 在故事板中的NavBar下創建另一個UIView,然後將textField和其他UI對象放在這個新視圖中。 保持此狀態這是介意navBar不應位於顯示鍵盤時向上推動的相同UIView對象。 因此,創建這個新創建的UIView對象的出口,而不是移動viewController的主視圖,只是模式像這樣新創建的視圖。

[UIView animateWithDuration:0.2 animations:^{ 
    //Just Replace Your Animation Code with This and see the difference. 
    self.containerViewOfOtherObjects.frame = CGRectOffset(self.containerViewOfOtherObjects.frame, 0, movement); 
}]; 
+0

謝謝Syed,我已經添加了,我是否需要將'self.view.frame = CGRectOffset(self.view.frame,0,movement)'這行代替?或者也刪除'[UIView beginAnimations:@「anim」context:nil];'...... – CGR

+0

沒有,只是這一行。 'self.view.frame = CGRectOffset(self.view.frame,0,movement);' –

+0

我已經嘗試過了,但它很快就會向上移動,並且很快地將新增加的視圖移到原始位置 – CGR