在显示UIWebView的键盘时向上移动UIView

时间:2014-10-13 11:29:32

标签: ios objective-c iphone uiview uiwebview

当用户触摸webview中的文本字段时,我试图移动UIView,因为键盘将覆盖webview,用户将无法输入文本,我的代码工作得很好!在iOS 7上!但iOS 8视图向上移动,但当用户选择另一个文本字段(在webview中)时,UIView向下移动到初始位置! 。这是我的代码:

/* _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];


    }

}

我也尝试了UIKeyboardDidHideNotificationUIKeyboardDidShowNotification,但没有成功!

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>

3 个答案:

答案 0 :(得分:1)

在iOS 8中,最好使用keyboardFrameDidChange而不是UIKeyboardWillShowNotification

添加一个监听器:

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

实施选择器:

-(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];
}];
}

答案 1 :(得分:1)

由于我不知道您的约束是如何设置的,因此很难调整您的视图的特定问题。你真的不应该发出一个setFrame调用,尤其是高度和其他值的固定数字!

Sreejith已经指出,你可以通过这种方式获得键盘尺寸:

CGSize keyboardSize = [[[notif userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

如果您不想直接更改约束(不要忘记您可以IBOutlet它们并设置常量),您可以尝试使用以下内容更改边缘插入:

self.scrollView.contentInset = UIEdgeInsetsMake(self.topLayoutGuide.length, 0, self.view.height - keyboardSize.origin.y, 0);

动画时间也应该从键盘信息中删除:

NSTimeInterval animationDuration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

如果这些建议不起作用,链接到演示项目将有助于调试。

答案 2 :(得分:0)

我通过设置webView的autoresizingMask

解决了同样的问题
_webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;