好吧,我已经经历了关于SO和教程的几乎所有问题。我现在可以将我的UIView
向上移动,以便在打字时可以看到它。但问题是,如果UITextField
被键盘覆盖,我想要移动UIView
只有,因为如果不是,那么在移动UIView
时没有意义。我的代码到目前为止:当键盘出现时向上移动UIView
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -keyboardSize.height;
self.view.frame = f;
}];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
任何帮助将不胜感激,谢谢。
是你提交的文字和键盘挣扎?使用https://github.com/michaeltyson/TPKeyboardAvoiding –
@Tutu如果你使用autolayout比这个链接可能对你有所帮助.. http://stackoverflow.com/questions/31356293/uitableview-and-uiview-with- keyboardwillshow/31356527#31356527 –