我想调整WKWebView的大小以适合横向和纵向键盘上方,并应对用户在使用键盘时在横向和纵向之间切换的问题。我还希望在键盘消失时重新调整WKWebView的大小。
默认行为是滚动WKWebView,以使正在使用的文本字段在键盘上方可见。我想替代此行为。
我希望我必须添加一些代码来响应keyboardDidShow和keyboardDidHide,以更改WebView使用的GCRect。这些是正确的事件吗?
如何以纵向和横向访问键盘的高度?
我是否必须为此添加一个延迟,否则在KeyboardDidShow触发时,Web视图的默认滚动是否已经发生?
当我更改Webview GCRect大小时,此触发window.onresize是否会在JavaScript中显示?
这样做会允许在使用键盘时旋转设备吗?还是需要添加更多代码来处理此问题?
感谢任何人都能给我的帮助。
答案 0 :(得分:0)
为解决这个问题,我在ViewController.h中添加了以下声明
re.sub('\w+ man ', '', t)
我希望我应该已经将这些声明为ViewController对象的属性,但这与答案有关
然后我按如下方式创建网络视图
int appH ; // device width
int appW ; // device height
int keyH ; // keyboard height
int topMargin ; // safe area size at top of screen
int bottomMargin ; // safe area size at bottom of screen
bool smallScreen ; // whether want to see status bar in landscape
然后我对尺寸的变化做出如下反应
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
NSString *deviceType = [UIDevice currentDevice].model;
smallScreen = [deviceType hasPrefix:@"iPhone"];
WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
[theConfiguration.userContentController addScriptMessageHandler:self name:@"MyApp"];
[theConfiguration.preferences setValue:@"TRUE" forKey:@"allowFileAccessFromFileURLs"];
// note that in viewDidLoad safeAreaInsets is set to zero
topMargin = [UIApplication sharedApplication].statusBarFrame.size.height ;
if (@available(iOS 11, *)) topMargin = self.view.safeAreaInsets.top ;
bottomMargin = 0 ;
if (@available(iOS 11, *)) bottomMargin = self.view.safeAreaInsets.bottom ;
int top = (smallScreen && appW > appH)? 0 : topMargin ;
int height = (smallScreen && appW > appH)? appH : appH - topMargin - bottomMargin ;
webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, top , appW, height) configuration:theConfiguration];
webView.navigationDelegate = self;
webView.UIDelegate = self;
我还注册了以下键盘事件通知
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
[super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
// code here executes before rotation
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
// code here executes during rotation
appH = size.height ;
appW = size.width ;
int top = (smallScreen && appW > appH)? 0 : topMargin ;
int height = (smallScreen && appW > appH)? appH : appH - topMargin - bottomMargin ;
webView.frame = CGRectMake(0, top , appW, height);
}
completion:^(id<UIViewControllerTransitionCoordinatorContext> context)
{
// code here executes after rotation
[webView evaluateJavaScript:@"IOSOrientationChanged()" completionHandler:nil];
}
];
}
以这种方式对这些做出反应
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
在以上各节中,我提到了两个Javascript函数
IOSOrientationChanged根据window.innerWidth和window.innerHeight中找到的屏幕大小来重绘应用程序内容。
IOSKeyboard Changed的功能相同,但是使用window.innerHeight减去键盘高度(作为唯一参数传递给它)。
注意事项