I am trying to move an UIView when user touch a textfield in a webview , because keyboard will cover the webview and users will not able to enter text , my codes works very fine ! on iOS 7 !!! but iOS 8 the view moves up but when user select another textfield (in webview) the UIView moves down to the initial position !!! . here is my code :
/* _contactForm = UIWebView
_contactView = UIView */
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)keyboardDidShow: (NSNotification *) notif {
[[[_contactForm subviews] lastObject] setScrollEnabled:YES];
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
[UIView animateWithDuration:.20
delay:0
options:UIViewAnimationOptionCurveLinear animations:^
{
[_contentView setFrame:CGRectMake(0, 37 , _contentView.frame.size.width, _contentView.frame.size.height)];
} completion:nil];
}
}
- (void)keyboardDidHide: (NSNotification *) notif{
[[[_contactForm subviews] lastObject] setScrollEnabled:NO];
[UIView animateWithDuration:.20
delay:0
options:UIViewAnimationOptionCurveLinear animations:^
{
[_contentView setFrame:CGRectMake(0, 176 , _contentView.frame.size.width, _contentView.frame.size.height)];
}completion:nil];
}
}
I also tried UIKeyboardDidHideNotification
and UIKeyboardDidShowNotification
, but no success !
WebView loads an HTML file from bundle , this is textfield's codes :
<form action="http://someurl.net/mail/mail.cshtml" method="post" onsubmit="return validate();">
<input class="textInput" type="text" name="name" placeholder="NAME"/>
<div class="clearfix"></div>
<input class="textInput" type="email" name="email" placeholder="EMAIL"/>
<div class="clearfix"></div>
<input class="textInput" type="text" name="phone" placeholder="PHONE"/>
<div class="clearfix"></div>
<textarea name="msg" cols="19" rows="3" placeholder="MESSAGE"></textarea>
<div class="clearfix"></div>
<input type="submit" class="submit" value="SEND"/>
</form>
In iOS 8 its better to use keyboardFrameDidChange
rather than UIKeyboardWillShowNotification
Add a listener :
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameDidChange:)
name:UIKeyboardDidChangeFrameNotification object:nil];
Implement the selector :
-(void)keyboardFrameDidChange:(NSNotification*)notification{
NSDictionary* info = [notification userInfo];
CGRect kKeyBoardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:0.25f delay:0 options:[[[notification userInfo] objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue] animations:^{
// Do your animation
} completion:^(BOOL finished) {
[yourView layoutIfNeeded];
}];
}
It's hard to debug the specific issue with your views since I don't know how your constraints are set up. You really shouldn't be issuing a setFrame call, especially with fixed numbers for the height and other values!
It's already been pointed out by Sreejith that you can get the keyboard size this way:
CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
If you don't want to change the constraints directly (don't forget you can IBOutlet them and set the constant) you can try altering the edge insets with something like this:
self.scrollView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.view.height - keyboardSize.origin.y, 0);
The animation time should also be driven off they keyboard info:
NSTimeInterval animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
Linking to a demo project would help a lot in debugging if these suggestions do not work.
我通过设置webView的autoresizingMask解决了相同的问题
_webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.