Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios] fix textarea offset by keyboard show #3297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion ios/sdk/WeexSDK/Sources/Component/WXEditComponent.mm
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,36 @@ - (void)setViewMovedUp:(BOOL)movedUp
self.weexInstance.rootView.frame = rect;
}

// UITextView键盘弹起偏移量
- (void)setTextViewMovedUp
{
UIView *rootView = self.weexInstance.rootView;
CGRect rect = self.weexInstance.frame;
CGRect rootViewFrame = rootView.frame;
CGRect inputFrame = [self.view.superview convertRect:self.view.frame toView:rootView];

CGFloat inputOffset = inputFrame.size.height - (rootViewFrame.size.height - inputFrame.origin.y);

UITextView *textView = (UITextView *)self.view;
CGRect cursorRect = [textView caretRectForPosition:textView.selectedTextRange.end];
CGRect cursorRectInWindow = [textView convertRect:cursorRect toView:rootView];
CGFloat textViewOffsetY = cursorRectInWindow.origin.y + cursorRectInWindow.size.height;
CGFloat keyboardOffsetY = rootView.frame.size.height - self.keyboardSize.height;
if (textViewOffsetY >= keyboardOffsetY) {
CGFloat offset = textViewOffsetY - keyboardOffsetY + _upriseOffset;

if (offset > 0) {
rect = (CGRect){
.origin.x = 0.f,
.origin.y = rect.origin.y - offset,
.size = rootViewFrame.size
};
}

self.weexInstance.rootView.frame = rect;
}
}

#pragma mark textview Delegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
Expand All @@ -792,6 +822,10 @@ - (BOOL)textViewShouldBeginEditing:(UITextView *)textView

- (void)textViewDidBeginEditing:(UITextView *)textView
{
// 此处获取光标位置时需延迟0.1s,否则获取的位置是上一次的光标位置
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self setTextViewMovedUp];
});
_changeEventString = [textView text];
if (_focusEvent) {
[self fireEvent:@"focus" params:nil];
Expand All @@ -817,6 +851,7 @@ - (void)textViewDidChange:(UITextView *)textView

- (void)textViewDidEndEditing:(UITextView *)textView
{
[self setTextViewMovedUp];
if (![textView.text length]) {
[self setPlaceholderAttributedString];
}
Expand Down Expand Up @@ -997,7 +1032,10 @@ - (void)keyboardWasShown:(NSNotification*)notification
};
CGRect inputFrame = [self.view.superview convertRect:self.view.frame toView:rootView];
if (keyboardRect.origin.y - inputFrame.size.height <= inputFrame.origin.y) {
[self setViewMovedUp:YES];
// textview在delegate中处理
if (![self.view isKindOfClass:UITextView.class]) {
[self setViewMovedUp:YES];
}
self.weexInstance.isRootViewFrozen = YES;
}
}
Expand Down