2016-12-29 40 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