Petrash 的解决方案对我有用。但我仍然在 iPad 上支持旋转时遇到问题。所以,在同一个 CDVViewController.m 中,我添加了这个方法:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
if (self.webView){
CGRect newFrame = self.webView.bounds;
//NSLog(@"%f" , newFrame.size.height);
NSString *JS = [NSString stringWithFormat:@"viewport = document.querySelector('meta[name=viewport]'); viewport.setAttribute('content', 'user-scalable=no, initial-scale=1.0, maximum-scale=1, minimum-scale=1, width=device-width, height=%d, target-densitydpi=device-dpi');", (int) newFrame.size.height*1 ];
[self.webView stringByEvaluatingJavaScriptFromString:JS];
}
}
并且,为了支持“非规模”行为,以这种方式编辑了 Petrash 的解决方案:
CGRect newFrame = self.webView.bounds;
//NSLog(@"%f" , newFrame.size.height);
NSString *JS = [NSString stringWithFormat:@"viewport = document.querySelector('meta[name=viewport]'); viewport.setAttribute('content', 'user-scalable=no, initial-scale=1.0, maximum-scale=1, minimum-scale=1, width=device-width, height=%d, target-densitydpi=device-dpi');", (int) newFrame.size.height*1 ];
[self.webView stringByEvaluatingJavaScriptFromString:JS];
键盘收缩视图 = 假
这是 hacky,但它适用于 5.1 到 7.0.3。在 Cordova 3.0 上测试。