在我的iPad应用程序中,我的textView
和textField's
很少。当我点击textField
时,键盘会覆盖textField
。所以我正在实现以下代码来移动textview。但在轮换到portraitUpsideDown
时,其工作不正常。它以相反的方向向下滑动屏幕。那么我该如何解决这个问题?
-(void) animateTextField: (UITextView *) textField up: (BOOL) up
{
int txtPosition = (textField.frame.origin.y - 540);
const int movementDistance = (txtPosition < 0 ? 0 : txtPosition); // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
-(void)textViewDidBeginEditing:(UITextView *)textField
{
[self animateTextField: textField up: YES];
}
-(void)textViewDidEndEditing:(UITextView *)textField
{
[self animateTextField: textField up: NO];
}
-(BOOL)textFieldShouldReturn:(UITextView *)theTextField
{
[theTextField resignFirstResponder];
return YES;
}
答案 0 :(得分:0)
如果您的方法是这样的。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
试试这个。我完全不知道。但我想帮助你。可能x
和y
坐标无法在任何方向更改。试试这个。
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation=UIInterfaceOrienationPotraitUpsideDown){
//Declare txtPos globally...
txtPos=(textField.frame.origin.y + 540);
}
if(interfaceOrientation=UIInterfaceOrienationPotrait)
{
txtPos=(textField.frame.origin.y - 540);
}
return(YES);
}
在动画方法中。
将textPos
分配给txtPosition
变量..
答案 1 :(得分:0)
此解决方案适用于iPhone,但它同时考虑两种方向。
你可以适应一点,瞧瞧:
http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html
答案 2 :(得分:0)
疯狂,
只需添加其他功能:
- (void) animateTextView: (UITextView*) textView up: (BOOL) up
{
const int movementDistance = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
然后称之为:
- (void)textViewDidBeginEditing:(UITextView *)textView {
[self animateTextView: textView up: YES];
}
- (void)textViewDidEndEditing:(UITextView *)textView {
[self animateTextView: textView up: NO];
}
答案 3 :(得分:0)
您应该使用键盘显示并隐藏通知以捕获键盘事件并相应地调整您的视图。
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification {
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
UIViewAnimationOptions animationOption = animationCurve << 16;
[UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
// adjust height using keyboardHeight
} completion:^(BOOL finished) {
}];
}
- (void)keyboardWillHide:(NSNotification *)notification {
CGRect keyboardFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = CGRectGetHeight(keyboardFrame);
CGFloat animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
UIViewAnimationCurve animationCurve = [notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
UIViewAnimationOptions animationOption = animationCurve << 16;
[UIView animateWithDuration:animationDuration delay:0 options:animationOption animations:^{
// adjust height using keyboardHeight
} completion:^(BOOL finished) {
}];
}
此博客文章详细解释了这一点
http://charlie.cu.cc/2015/10/solution-to-the-ios-software-keyboard-cover-part-of-the-ui/