Resize UIKeyBoard Orientation When Changing Landscape Orientation

This is a very newbie. I have a UIToolBar at the bottom, which should be animatedly moving up and down using the keyboard when the UIKeyBoard is displayed. I got this working with UIKeyBoard Notifications. The view we are talking about has a permitted section view. When the orientation of the device is landscape, both column views are displayed [hope it makes sense].

When the keyboard is displayed, I do it

CGSize keyBoardSize = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue].size;

CGRect toolbarFrame= [BottomToolBar frame];
toolbarFrame.origin.y -= keyBoardSize.height;    
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
BottomToolBar .frame = viewFrame;
[UIView commitAnimations];

when the keyboard is welcome, I do it

toolbarFrame.origin.y += keyBoardSize.height;

, , , . , . , . - ? , , ?

+3
6

, , , ( , ). .

, . ( .)

CGRect keyboardRect = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
keyboardRect = [[BottomToolBar superview] convertRect:keyboardRect fromView:nil];

UPDATE: UIKeyboardWillShowNotification, , :)

: https://devforums.apple.com/message/181482#181482

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

- (void)keyboardWillShow:(NSNotification *)aNotification 
{
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    CGFloat keyboardHeight;
    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            keyboardHeight = keyboardBounds.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            keyboardHeight = keyboardBounds.size.width;
            break;
    }

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect rect = table.frame;
    rect.size.height -= keyboardHeight;
    [UIView beginAnimations:@"ResizeForKeyboardShow" context:nil];
    [UIView setAnimationDuration:animationDuration];
    table.frame = rect;
    [UIView commitAnimations];
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    CGRect keyboardBounds;
    [[aNotification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];

    CGFloat keyboardHeight;
    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:
            keyboardHeight = keyboardBounds.size.height;
            break;
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            keyboardHeight = keyboardBounds.size.width;
            break;
    }

    NSTimeInterval animationDuration = [[[aNotification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    CGRect rectTable = table.frame;
    rectTable.size.height += keyboardHeight;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    table.frame = rectTable;
    [UIView commitAnimations];
}
+6

UITextFields inputAccessoryView, UIToolBar UIKeyboard.

+4
- (void)viewDidLoad { // Or somewhere else
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbDidChange:) UIKeyboardDidChangeFrameNotification object:nil];
}

- (void)kbDidChange:(NSNotification *)notification {
    NSDictionary* keyboardInfo = [notification userInfo];
    CGRect keyboardFrame = [[keyboardInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    keyboardFrame = [[messageBar superview] convertRect:keyboardFrame fromView:nil];

    CGRect ol = messageBar.frame; // messageBar is your UIToolbar..
    ol.origin.y = keyboardFrame.origin.y-44;
    messageBar.frame = ol;
}
+2

, . , , "height" , , - , , , - ( ).

, , - , "y" (, , ).

0

, UIViewAnimationOptionBeginFromCurrentState

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
BottomToolBar.frame = viewFrame;
[UIView commitAnimations];

in your case. Then the view will not have a sudden movement when the orientation changes from portrait to landscape.

0
source

Source: https://habr.com/ru/post/1737633/


All Articles