Moving dialog up when keyboard present works except when ipad is turned around

。_饼干妹妹 提交于 2019-12-13 19:09:49

问题


The modal dialog gets moved up when they keyboard appears and moves down when the keyboard disappears.

All is fine till I rotate the iPad. In any other orientation except the standard it doesn't work. When the iPad is turned around the modal dialog moves down when the keyboard appears instead of up and up when the keyboard disappears instead of down.

This is the code I am using to position the modal dialog when keyboard appears/disappears.

- (void)textFieldDidBeginEditing:(UITextField *)textField {


        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 140, self.view.superview.frame.size.width, self.view.superview.frame.size.height);

        }      
    }];

}


-(void)textFieldDidEndEditing:(UITextField *)textField {

    [UIView animateWithDuration:0.4 animations:^ {

        self.view.superview.frame = CGRectMake(self.view.superview.frame.origin.x, 212, self.view.superview.frame.size.width, self.view.superview.frame.size.height);
        }
    }];

}

回答1:


Instead of setting the frame, use CGAffineTransformTranslate, for example like so:

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,72);
    }      
}];
}


-(void)textFieldDidEndEditing:(UITextField *)textField {
[UIView animateWithDuration:0.4 animations:^ {
    self.view.superview.transform = CGAffineTransformTranslate(self.view.superview.transform,0,-72);
    }
}];
}



回答2:


You should try using Keyboard Notifications:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillBeDismissed:) name:UIKeyboardWillHideNotification object:nil];

and then in the selectors adjust the frame. Not in textFieldDidBeginEditing/textFieldDidEndEditing.

- (void)keyboardWasShown:(NSNotification *) notification {
    NSDictionary *info = [notification userInfo];
    NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    keyboardHeight = MIN(keyboardSize.height, keyboardSize.width);
    // set new frame based on keyboard size 

- (void)keyboardWillBeDismissed: (NSNotification *) notification{
    [UIView animateWithDuration:0.4 animations:^{
    // original frame 
    }];
}


来源:https://stackoverflow.com/questions/16104251/moving-dialog-up-when-keyboard-present-works-except-when-ipad-is-turned-around

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!