Skip to content

Commit

Permalink
* [ios] fix keyboard cover input sometime
Browse files Browse the repository at this point in the history
  • Loading branch information
acton393 committed Sep 21, 2016
1 parent 45d33a2 commit 0ad56dc
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXTextInputComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "WXTextInputComponent.h"
#import "WXConvert.h"
#import "WXUtility.h"
#import "WXSDKInstance.h"

@interface WXTextInputView : UITextField
@property (nonatomic, assign) UIEdgeInsets border;
Expand Down Expand Up @@ -67,6 +68,8 @@ @interface WXTextInputComponent()
@property (nonatomic) BOOL changeEvent;
@property (nonatomic) BOOL clickEvent;
@property (nonatomic, strong) NSString *changeEventString;
@property (nonatomic, assign) CGSize keyboardSize;
@property (nonatomic, assign) CGRect rootViewOriginFrame;

@end

Expand Down Expand Up @@ -190,9 +193,28 @@ - (void)viewDidLoad
_inputView.inputAccessoryView = toolbar;
}

- (void)viewWillLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}

- (void)viewWillUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:_inputView];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];

[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

#pragma mark - Add Event
Expand Down Expand Up @@ -394,6 +416,31 @@ - (void)textFiledEditChanged:(NSNotification *)notifi{
}
}

- (void)setViewMovedUp:(BOOL)movedUp
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2]; //slide up the view
UIView *rootView = self.weexInstance.rootView;
CGRect rect = rootView.frame;
CGFloat offset = _keyboardSize.height - CGRectGetMaxY(rect) + CGRectGetMaxY(_inputView.frame);
if (movedUp) {
if (offset > 0) {
rect = (CGRect){
.origin.x = 0.f,
.origin.y = -offset,
.size = rootView.frame.size
};
}
}else {
// revert back to the origin state
rect = _rootViewOriginFrame;
}
self.weexInstance.rootView.frame = rect;

[UIView commitAnimations];
}


#pragma mark

- (void)setPlaceholderAttributedString
Expand Down Expand Up @@ -463,6 +510,33 @@ - (void)setBorder:(UIEdgeInsets)border
[_inputView setBorder:border];
}

#pragma mark keyboard
- (void)keyboardWasShown:(NSNotification*)notification
{
// Animate the current view out of the way
_keyboardSize= [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIView * rootView = self.weexInstance.rootView;
CGRect screenRect = [[UIScreen mainScreen] bounds];
_rootViewOriginFrame = rootView.frame;
CGRect keyboardRect = (CGRect){
.origin.x = 0,
.origin.y = CGRectGetMaxY(screenRect) - _keyboardSize.height,
.size = _keyboardSize
};
CGRect inputFrame = _inputView.frame;
if (keyboardRect.origin.y - inputFrame.size.width <= inputFrame.origin.y) {
[self setViewMovedUp:YES];
}
}

- (void)keyboardWillHide:(NSNotification*)notification
{
UIView * rootView = self.weexInstance.rootView;
if (rootView.frame.origin.y < 0) {
[self setViewMovedUp:NO];
}
}

- (void)closeKeyboard
{
[_inputView resignFirstResponder];
Expand Down

0 comments on commit 0ad56dc

Please sign in to comment.