【问题标题】:Moving up an UIView when UIWebView's keyboard is shown显示 UIWebView 的键盘时向上移动 UIView
【发布时间】:2014-10-13 11:29:31
【问题描述】:

当用户触摸 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 文件,这是文本字段的代码:

<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>

【问题讨论】:

  • 你能给我网址吗。
  • @KiritModi 请检查编辑后的答案
  • 能否请您尝试在隐藏和显示方法上设置断点。我想知道当用户从一个字段点击到另一个字段时两者是否都会被调用。

标签: ios objective-c iphone uiview uiwebview


【解决方案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];
}];
}

【讨论】:

  • @Mc.Lover 你在使用自动布局吗?您的视图是否固定在底部?
  • 是的,涨了!但是通过在 webView 上选择另一个文本字段,它会回到原来的位置!我的 webview 是联系表单有不同的文本字段
  • 只是一个建议。如果启用了自动布局,那么您应该通过调整其约束来更改视图的位置,而不是设置框架。
  • @Mc.Lover 有帮助吗?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

    _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    

    【讨论】:

      猜你喜欢
      • 2011-07-10
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 1970-01-01
      • 2020-10-28
      • 2014-11-11
      • 2018-12-16
      相关资源
      最近更新 更多