gpt4 book ai didi

ios - 如何计算 iOS 中的键盘高度?

转载 作者:行者123 更新时间:2023-11-28 18:10:05 24 4
gpt4 key购买 nike

我知道这个问题被问过很多次,但我不知道我在哪里犯了错误。简而言之,我无法正确实现它。

我的应用程序必须在 iPhone 和 iPad 上运行(仅限纵向)。我有一个子类 UITextField。在编辑过程中它被键盘隐藏时,我需要将它向上移动。我无法正确设置它,因为键盘大小的计算是在进入编辑模式后完成的。

我知道我需要执行以下步骤:

  1. 注册观察员
  2. 有计算键盘矩形的例程,我可以从中获取高度
  3. 在键盘的显示和隐藏上设置 2 个事件来偏移我的自定义文本字段。

请告诉我哪里错了。

我的代码是:

注册观察者:

- (id)initWithCoder:(NSCoder *)aDecoder{
if (self = [super initWithCoder:aDecoder]) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChange:) name:UIKeyboardDidShowNotification object:nil];
_leftInlineImage = NO;
}
return self;
}

计算键盘高度:

- (void)keyboardWillChange:(NSNotification *)notification {
NSDictionary* d = [notification userInfo];
CGRect r = [d[UIKeyboardFrameEndUserInfoKey] CGRectValue];
r = [self.superview convertRect:r fromView:nil];
_keyboardHeight = r.size.height;
}

显示/隐藏键盘:

/* Move keyboard */
-(void)showKeyboardWithMove {

CGRect screenRect = [[UIScreen mainScreen] bounds];
////CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

//_keyboardHeight = KEYBOARD_HEIGHT;

if (self.frame.origin.y + self.frame.size.height > screenHeight - _keyboardHeight) {
double offset = screenHeight - _keyboardHeight - self.frame.origin.y - self.frame.size.height;
CGRect rect = CGRectMake(0, offset, self.superview.frame.size.width, self.superview.frame.size.height);

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

self.superview.frame = rect;

[UIView commitAnimations];
}

}

-(void)hideKeyboardWithMove {

CGRect rect = CGRectMake(0, 0, self.superview.frame.size.width, self.superview.frame.size.height);

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];

self.superview.frame = rect;

[UIView commitAnimations];

}

之前计算过,我的高度是0。这一切都是在UITextField的子类中完成的。

最佳答案

UIKeyboardWillChangeFrameNotification的观察者添加到viewDidLoad

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

这个方法可以帮助你:

- (void)keyboardFrameDidChange:(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];
}

关于ios - 如何计算 iOS 中的键盘高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27245935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com