繁体   English   中英

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

[英]Moving up an UIView when UIWebView's keyboard is shown

当用户触摸Web视图中的文本字段时,我试图移动UIView,因为键盘将覆盖Web视图,并且用户将无法输入文本,我的代码工作得很好! 在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>

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

由于我不知道如何设置约束,因此很难用您的视图调试特定问题。 您确实不应该发出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];

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

我通过设置webView的autoresizingMask解决了相同的问题

_webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM