阻止iOS通话状态栏将PhoneGap UIWebView推离屏幕 [英] Prevent iOS in-call status bar from pushing Phonegap UIWebView offscreen
问题描述
我的问题是这样的:每当一个iPhone用户在通话,或者正在使用他或她的手机作为热点,iOS 7状态栏被放大,从而推动我的PhoneGap应用程序的UIWebView从屏幕底部。放大的状态栏称为通话状态栏。如下图所示:
My issue is this: whenever an iPhone user is in call, or is using his or her phone as a hotspot, the iOS 7 status bar is enlarged, thus pushing my Phonegap application's UIWebView off the bottom of the screen. The enlarged status bar is termed the "in-call status bar". See below image:
Stack Overflow的答案我试图补救这个:
Stack Overflow answers I have tried to remedy this:
Iphone-如何在调用状态栏切换时调整视图大小? / a>
Iphone- How to resize view when call status bar is toggled?
How In-Call status bar impacts UIViewController's view size ? (and how to handle it properly)
此外,似乎没有任何类型的事件触发Phonegap通知我状态栏的更改。收听Phonegap暂停事件是无用的,如1)它已知在iOS中有怪癖和2)它不真正覆盖热点情况。
Additionally, there does not seem to be any sort of event fired by Phonegap that informs me of the status bar's change. Listening to the Phonegap "pause" event is useless, as 1) it's known to have quirks in iOS and 2) it doesn't really cover the hotspot case.
我的Objective-C技能非常小,只有在提出要求的4+小时后,才会提出这样的问题:Googling,Stack Overflowing,wailing等...
My Objective-C skills are very minimal, and I only resort to asking this sort of question after putting in the requisite 4+ hours Googling, Stack Overflowing, wailing, etc...
堆栈的神溢出,给我
推荐答案
根据Jef的建议,制作以下解决方案。您要做的是以下操作:
Came up with the following solution based on Jef's suggestions. What you'll want to do is the following:
- 观察原生
didChangeStatusBarFrame
委托 - 通过原生
statusBarFrame获取状态栏的大小信息
- 通过触发传递它的事件。
- Observe the native
didChangeStatusBarFrame
delegate - Get size information about the statusbar via native
statusBarFrame
- Expose information to your webview by triggering an event that passes it
I have setup a Github repo with all the code you find in this answer.
在AppDelegate中设置通知
Setup notification in 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];
}
Make statusBarChange
selector可用
Make statusBarChange
selector available
// MainViewController.h
@protocol StatusBarChange <NSObject>
-(void)onStatusbarChange:(NSNotification*)notification;
@end
设置侦听器。这会得到 origin
和 size
字典从 statusBarFrame
更改并触发在传递此数据的webview中的事件。
Setup the listener. This gets the origin
and size
dictionaries from statusBarFrame
whenever it changes and fires an event in the webview passing along this data.
// 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
事件,例如像这样:
All this allows you to listen for window.statusbarchange
event, e.g. like this:
// www/js/index.js
window.addEventListener('statusbarchange', function(e){
// Use e.size.height to adapt to the changing status bar
}, false)
这篇关于阻止iOS通话状态栏将PhoneGap UIWebView推离屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!