У меня есть UIViewController
, представленный с использованием self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet;
, а теперь в iOS 8 последний UITextView
скрыт от клавиатуры, когда он будет выдвинут. Как его избежать?
Клавиатура iOS 8 скрывает мое текстовое изображение
Ответ 1
@Oleg решение хорошее, но оно не учитывает изменения клавиатуры в размере, пока он уже показывает. Кроме того, он жестко закодировал продолжительность анимации и несколько других параметров. См. Мое решение ниже:
Добавить наблюдателя UIKeyboardWillChangeFrameNotification
к viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
Затем добавьте метод:
- (void)keyboardFrameWillChange:(NSNotification *)notification
{
CGRect keyboardEndFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGRect keyboardBeginFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
UIViewAnimationCurve animationCurve = [[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] integerValue];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
CGRect newFrame = self.view.frame;
CGRect keyboardFrameEnd = [self.view convertRect:keyboardEndFrame toView:nil];
CGRect keyboardFrameBegin = [self.view convertRect:keyboardBeginFrame toView:nil];
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y);
self.view.frame = newFrame;
[UIView commitAnimations];
}
Не забудьте удалить наблюдателя клавиатуры как в viewDidUnload, так и в Dealloc.
Ответ 2
Для решения проблемы вы должны изменить frame
или constrain
textview
.
Небольшой совет для вас:
Использовать оповещение для обнаружения, когда клавиатура будет отображаться или скрываться.
Пример уведомления:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
И чем изменить один из параметров, упомянутых выше.
Пример изменения рамки:
- (void)keyboardWillHide:(NSNotification *)notification
{
[self.view layoutIfNeeded];
[UIView animateWithDuration:0.2
animations:^{
[self setStartingFrame:self.commentsView.frame.size.height - commentViewOutlet_.frame.size.height - tabBarOffset_];
[self.view layoutIfNeeded];
}];
}
Ответ 3
Быстрая версия @newton_guima ответьте выше, если кто-то захочет этого.
Добавить наблюдателя от UIKeyboardWillChangeFrameNotification
до viewDidLoad()
:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardFrameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)
Затем добавьте этот метод:
func keyboardFrameWillChange(notification: NSNotification) {
let keyboardBeginFrame = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue
let keyboardEndFrame = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameEndUserInfoKey)!.CGRectValue
let animationCurve = UIViewAnimationCurve(rawValue: (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardAnimationCurveUserInfoKey)!.integerValue)
let animationDuration: NSTimeInterval = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardAnimationDurationUserInfoKey)!.doubleValue
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(animationDuration)
UIView.setAnimationCurve(animationCurve!)
var newFrame = self.view.frame
let keyboardFrameEnd = self.view.convertRect(keyboardEndFrame, toView: nil)
let keyboardFrameBegin = self.view.convertRect(keyboardBeginFrame, toView: nil)
newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y)
self.view.frame = newFrame;
UIView.commitAnimations()
}
Затем удалите наблюдателя везде, где вы переходите от представления, или в deinit
:
NSNotificationCenter.defaultCenter().removeObserver(self)