根据Jef的建议,提出了以下解决方案。您需要执行以下操作:
- 观察本地
didChangeStatusBarFrame
代理
- 通过原生
statusBarFrame
获取状态栏的大小信息
- 通过触发传递事件来将信息公开给您的webview
我已经在
Github存储库中设置了所有代码。
在AppDelegate中设置通知。
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame
{
NSMutableDictionary *statusBarChangeInfo = [[NSMutableDictionary alloc] init];
[statusBarChangeInfo setObject:@"statusbarchange"
forKey:@"frame"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"statusbarchange"
object:self
userInfo:statusBarChangeInfo];
}
使
statusBarChange
选择器可用
@protocol StatusBarChange <NSObject>
-(void)onStatusbarChange:(NSNotification*)notification;
@end
设置监听器。每当状态栏框架(statusBarFrame)发生变化时,从中获取origin
和size
字典,并在webview中触发一个事件,将这些数据传递给它。
- (void)onStatusbarChange:(NSNotification*)notification
{
NSMutableDictionary *eventInfo = [self getStatusBarInfo];
[self notifiy:notification.name withInfo:eventInfo];
}
- (void)notifiy:(NSString*)event withInfo:(NSMutableDictionary*)info
{
NSString *json = [self toJSON:info];
NSString *cmd = [NSString stringWithFormat:@"cordova.fireWindowEvent('\%@\', %@)", event, json];
[self.webView stringByEvaluatingJavaScriptFromString:cmd];
}
- (NSMutableDictionary *)getStatusBarInfo
{
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
NSMutableDictionary *statusBarInfo = [[NSMutableDictionary alloc] init];
NSMutableDictionary *size = [[NSMutableDictionary alloc] init];
NSMutableDictionary *origin = [[NSMutableDictionary alloc] init];
size[@"height"] = [NSNumber numberWithInteger:((int) statusBarFrame.size.height)];
size[@"width"] = [NSNumber numberWithInteger:((int) statusBarFrame.size.width)];
origin[@"x"] = [NSNumber numberWithInteger:((int) statusBarFrame.origin.x)];
origin[@"y"] = [NSNumber numberWithInteger:((int) statusBarFrame.origin.y)];
statusBarInfo[@"size"] = size;
statusBarInfo[@"origin"] = origin;
return statusBarInfo;
}
- (NSString *) toJSON:(NSDictionary *)dictionary {
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&error];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
所有这些都可以让你监听
window.statusbarchange
事件,例如:
window.addEventListener('statusbarchange', function(e){
}, false)