我的问题是这样的:每当一个iPhone用户在通话或正在使用他或她的手机作为热点时,iOS 7的状态栏将被放大,从而将我的PhoneGap应用程序的UIWebView从屏幕底部推开.放大的状态栏称为“通话状态栏”.见下图:
堆栈溢出的答案我已经尝试补救这一点:
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似乎没有任何类型的事件通知我状态栏的更改.聆听电话间隙“暂停”事件是无用的,如1)it’s known to have quirks in iOS和2)它并没有真正涵盖热点情况.
我的Objective-C技能非常小,我只需要提出这样的问题,然后再提供4小时的谷歌搜索,堆栈溢出,哭泣等等.
堆栈溢出的神,向我发出你的巨大的书呆子愤怒.
解决方法
根据Jef的建议,提出以下解决方案.你想要做的是以下几点:
>观察本机didChangeStatusBarFrame委托
>通过native statusBarFrame获取有关状态栏的大小信息
通过触发通过它的事件,将信息暴露给您的Webview
我已经设置了一个Github repo与你在这个答案中找到的所有代码.
在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
设置侦听器.当它更改并触发通过此数据的WebView中的事件时,它会从statusBarFrame获取原始和大小字典.
// 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)