[英]Moving up an UIView when UIWebView's keyboard is shown
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 ! 当用户触摸Web视图中的文本字段时,我试图移动UIView,因为键盘将覆盖Web视图,并且用户将无法输入文本,我的代码工作得很好! on iOS 7 !!! 在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 !!! 但是在iOS 8中,视图向上移动,但是当用户选择另一个文本字段(在Webview中)时,UIView向下移动到初始位置! . 。 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 ! 我也尝试了UIKeyboardDidHideNotification
和UIKeyboardDidShowNotification
,但是没有成功!
WebView loads an HTML file from bundle , this is textfield's codes : WebView从bundle加载HTML文件,这是textfield的代码:
<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
在iOS 8中,最好使用keyboardFrameDidChange
而不是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! 您确实不应该发出setFrame调用,尤其是使用固定数字表示高度和其他值时!
It's already been pointed out by Sreejith that you can get the keyboard size this way: Sreejith已经指出,您可以通过以下方式获得键盘尺寸:
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: 如果您不想直接更改约束(不要忘记可以使用IBOutlet并设置常量),则可以尝试使用以下方法来更改边缘插值:
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.