【问题标题】:How to show Toolbar above Keyboard on UIWebView [closed]如何在 UIWebView 上的键盘上方显示工具栏 [关闭]
【发布时间】:2016-09-01 12:44:15
【问题描述】:

我有 Webview,下面有一个工具栏。当键盘出现时,我想在键盘顶部显示工具栏。如何做到这一点?

【问题讨论】:

  • 取决于您如何定义布局。如果您使用的是自动布局,那么我猜您有一个约束将工具栏的底部粘贴到主视图的底部。然后您可以在键盘出现时更改此约束的常量。如果您使用的是纯框架,您可以在键盘出现时更改工具栏的 y 原点。要检测键盘何时出现/下降,请参阅:stackoverflow.com/a/4374515/3844377
  • 我正在使用自动布局。你能指导一下当键盘出现时如何改变约束吗?
  • 在 UIWebview 上 inputAccessoryView 是只读的。

标签: ios xamarin.ios


【解决方案1】:

以下是实现您想要的基本架构:

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardDidShowNotification:)
                                                 name: UIKeyboardDidShowNotification
                                               object: nil];

    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(keyboardDidHideNotification:)
                                                 name: UIKeyboardDidHideNotification
                                               object:nil];
}


-(void) keyboardDidShowNotification: (NSNotification * ) notification {

    NSDictionary *dict   = [notification userInfo];
    CGRect keyboardFrame = [dict[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    self.view.frame = CGRectMake(0,0,
                                 self.view.frame.size.width,
                                 keyboardFrame.origin.y);
}


-(void) keyboardDidHideNotification: (NSNotification * ) notification {
    self.view.frame = [UIApplication sharedApplication].keyWindow.frame;
}

-(void) dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

希望对你有帮助。

【讨论】:

    【解决方案2】:

    我没有代码示例,但我可以用文字给出提示。 您需要为工具栏和屏幕底部(或放置它的任何位置)之间的高度创建一个NSLayoutConstraint。 然后在键盘出现后,您需要计算键盘的高度并调整工具栏高度的布局约束,使其高于键盘。然后在键盘关闭的时候再改回来。

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-03
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      相关资源
      最近更新 更多