klawiatura iOS 8 ukrywa mój widok tekstu

Mam UIViewController przedstawione za pomocą self.accountViewController.modalPresentationStyle = UIModalPresentationFormSheet; i teraz w iOS 8 Ostatni {[2] } jest ukryty przed klawiaturą, gdy zostanie przesunięty w górę. Jak tego uniknąć?

Author: FelixSFD, 2014-10-06

3 answers

@ Oleg rozwiązanie jest dobre, ale nie bierze pod uwagę zmian wielkości klawiatury, gdy już się pokazuje.. ponadto, na twardo zakodował czas trwania animacji i kilka innych parametrów. Zobacz moje rozwiązanie poniżej:

Dodaj obserwatora UIKeyboardWillChangeFrameNotification do viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameWillChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

A następnie dodać metodę:

- (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];
}

Nie zapomnij usunąć klawiatury observer zarówno w viewDidUnload, jak i w Dealloc.

 56
Author: newton_guima,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-28 17:47:09

Aby rozwiązać twój problem należy zmienić frame lub constrain textview.

Mała wskazówka dla ciebie:

Użyj powiadomienia, aby wykryć, kiedy klawiatura pokaże się lub ukryje.

Przykład zgłoszenia:

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

I następnie zmienić jeden z parametrów, które są wymienione powyżej.

Przykład zmiany ramki:

- (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
Author: Oleg Gordiichuk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-06 09:56:28

Swift Version of @ newton_guima ' s answer above incase anyone wants it.

Dodaj obserwatora z UIKeyboardWillChangeFrameNotification do viewDidLoad():

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardFrameWillChange:", name: UIKeyboardWillChangeFrameNotification, object: nil)

Następnie dodaj tę metodę:

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()
}

Następnie usuń obserwatora gdziekolwiek się oddalisz od widoku lub w deinit:

NSNotificationCenter.defaultCenter().removeObserver(self)
 3
Author: myles,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 10:31:09