键盘打开时iOS Webview检测?

时间:2015-11-18 10:44:17

标签: javascript html ios css webview

我在网页中有一个固定的页脚,我在iOS网页浏览中显示。在Mozilla / Chrome中,当键盘打开时,页脚会跳到键盘的顶部,这是理想的选择(我猜测在触发键盘时,webview本身会调整大小?)。

在Safari中它停留在键盘隐藏的底部,这有点烦人,因为我需要用户可以使用的继续按钮。因此,用户必须关闭键盘才能到达按钮,这是一种次优的体验。

同样在iOS网页浏览中,它也是隐藏的。

似乎理想的情况是在键盘显示时调整webview的高度(尽管我对其他想法/建议持开放态度)。

有没有人知道如何在键盘出现时观看,以便我可以根据键盘的尺寸调整全屏webivew的高度?

ViewController.m

#import "ViewController.h"

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

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSString *fullURL = @"localhost:3000/campaigns";
    NSURL *url = [NSURL URLWithString:fullURL];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:requestObj];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

2 个答案:

答案 0 :(得分:1)

这可能对您有所帮助

[[NSNotificationCenter defaultCenter] addObserver:self


                                     selector:@selector(keyboardWasShown:)

                                         name:UIKeyboardDidShowNotification

                                       object:nil];





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




// Get the size of the keyboard.

CGSize keyboardSize = [[[notification userInfo] 


objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


//Given size may not account for screen rotation

int height = MIN(keyboardSize.height,keyboardSize.width);

int width = MAX(keyboardSize.height,keyboardSize.width);

//finding screensize

CGRect screenRect = [[UIScreen mainScreen] bounds];

CGFloat screenWidth = screenRect.size.width;

CGFloat screenHeight = screenRect.size.height;


//Setting Screen Size


 CGRect frame = aWebView.frame;

    frame.size.height = "your height in int";

    aWebView.frame = frame;

    CGSize fittingSize = [aWebView sizeThatFits:CGSizeZero];

    frame.size = fittingSize;

    aWebView.frame = frame;

}

答案 1 :(得分:0)

在Swift 3中:

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidShow, object: nil, queue: nil) { (sender) in
    print("Keyboard did show")
}

这可能不止一次被召唤