防止iOS通话状态栏将Phonegap UIWebView推出屏幕

15
我的问题是:每当iPhone用户在通话或使用手机作为热点时,iOS 7状态栏都会变大,从而将我的Phonegap应用程序的UIWebView推出屏幕底部。放大的状态栏被称为“通话状态栏”。请参见下面的图片:

Wonky in-call status bar.

我尝试了以下 Stack Overflow 的答案来解决这个问题:

iPhone- 如何在调用状态栏切换时调整视图大小?

通话状态栏如何影响 UIViewController 的视图大小?(以及如何正确处理它)

此外,似乎没有任何 Phonegap 触发状态栏更改的事件。监听 Phonegap "pause" 事件是无用的,因为 1) 已知在 iOS 中存在缺陷,2) 它实际上并不涵盖热点情况。

我的 Objective-C 技能非常有限,在经过 4+ 小时的谷歌、Stack Overflow、哀嚎等必要步骤后,我才求助于这种类型的问题。

Stack Overflow 的神灵们,请向我施加你们丰富的技术之怒。


有一个 Cordova 插件,可以调整 IO7 状态栏。你可以在这里获取它:https://github.com/jota-v/cordova-ios-statusbar 或者 https://github.com/apache/cordova-labs/tree/plugins/statusbar - TorchMan
@ryan-miller 你使用的是哪个版本的PhoneGap? - Tomer Shiri
3.3.0 是我的当前版本。 - Ryan Miller
1
Stack Overflow的众神啊,请将你们丰富的技术怒火赐予我吧。这是我读过的最原创的“任何帮助都感激”的版本了! - marionebl
2个回答

9
根据Jef的建议,提出了以下解决方案。您需要执行以下操作:
  • 观察本地didChangeStatusBarFrame代理
  • 通过原生statusBarFrame获取状态栏的大小信息
  • 通过触发传递事件来将信息公开给您的webview
我已经在Github存储库中设置了所有代码。
在AppDelegate中设置通知。
// Appdelegate.m
- (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 选择器可用
// MainViewController.h
@protocol StatusBarChange <NSObject>
    -(void)onStatusbarChange:(NSNotification*)notification;
@end

设置监听器。每当状态栏框架(statusBarFrame)发生变化时,从中获取originsize字典,并在webview中触发一个事件,将这些数据传递给它。

// MainViewController.m
- (void)onStatusbarChange:(NSNotification*)notification
{
    // Native code for
    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事件,例如:

// www/js/index.js
window.addEventListener('statusbarchange', function(e){
  // Use e.size.height to adapt to the changing status bar      

}, false)

这个能否轻松地作为插件添加?由于缺少plugin.xml,常规的插件添加无法工作。 - Kevin Grabher

1

我想说,这种情况总是在你从后台返回时发生,对吗?

换句话说,如果没有您的应用程序被呼入电话等至少短暂地推到后台,那么栏是否可能扩大?

如果是这样,那么您肯定可以在代理的-(void)will(orDid)resume中查询状态栏高度并相应调整。

如果它确实发生而不离开前台,则会更加困难,我们需要确定要观察哪些通知,我知道有一个音频会话中断通知,用于呼入电话情况下,不确定关于热点的事情,但肯定也有通知……

编辑好了,这些是可供观察的通知之一。

UIApplicationWillChangeStatusBarFrameNotification

UIApplicationDidChangeStatusBarFrameNotification

或者在您的代理中实现其中一个回调函数

-application:willChangeStatusBarFrame:

-application:didChangeStatusBarFrame:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接