我使用SCLAlertView并使用UITextView添加自定义视图,我的问题是在第一次显示键盘时SCLAlertView没有移动,请看这张图片
我编写此代码以在键盘显示时移动SCLAlertView
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowFirstView:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideFirstView:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShowFirstView:(NSNotification *)notification
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect dialogRect = addAlert.view.frame;
CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGFloat top = (screenSize.height - keyboardHeight - dialogRect.size.height) / 2;
NSLog(@"show........: %.0f", top);
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = addAlert.view.frame;
frame.origin.y = top;
addAlert.view.frame = frame;
}];
}
- (void)keyboardWillHideFirstView:(NSNotification *)notification
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect dialogRect = addAlert.view.frame;
CGFloat top = (screenSize.height - dialogRect.size.height) / 2;
NSLog(@"...hide.....: %.0f", top);
[UIView animateWithDuration:0.2 animations:^{
CGRect frame = addAlert.view.frame;
frame.origin.y = top;
addAlert.view.frame = frame;
}];
}
现在当我隐藏&在模拟器中手动显示键盘" Command + K"它向右移动!!
有人可以帮忙解决这个问题!
答案 0 :(得分:0)
尝试将AlertView的 centerY 约束挂钩为 IBOutlet ,并在键盘显示/隐藏时更改常量
像这样 (void)keyboardWillShowFirstView:(NSNotification *)notification
{
CGSize screenSize = [UIScreen mainScreen].bounds.size;
CGRect dialogRect = addAlert.view.frame;
CGFloat keyboardHeight = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGFloat top = -1 * (keyboardHeight / 2.0);
self.alertViewCenterY.constant = top
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)keyboardWillHideFirstView:(NSNotification *)notification
{
self.alertViewCenterY.constant = 0
[UIView animateWithDuration:0.2 animations:^{
[self.view layoutIfNeeded];
}];
}
注意:如果需要,可以挂钩顶部或底部约束