Basierend auf den Vorschlägen von Jef kam die folgende Lösung auf. Was Sie tun möchten, ist die folgende:
- Beachten Sie die nativen
didChangeStatusBarFrame
Delegierten
- Get Größeninformationen über die Statusbar über native
statusBarFrame
- Expose Informationen zu Ihrem webview durch das Auslösen eines Ereignisses, das es passiert
Ich habe eine Github repo mit allen Code in dieser Antwort gefunden.
Setup-Benachrichtigung 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
Selektor verfügbar
// MainViewController.h
@protocol StatusBarChange <NSObject>
-(void)onStatusbarChange:(NSNotification*)notification;
@end
Richten Sie den Hörer. Dies ruft die origin
und size
Wörterbücher von statusBarFrame
, wann immer es ändert und löst ein Ereignis in der Webansicht, die diese Daten übergeben.
// 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];
}
All dies ermöglicht es Ihnen, für window.statusbarchange
Ereignis zu hören, zum Beispiel wie folgt:
// www/js/index.js
window.addEventListener('statusbarchange', function(e){
// Use e.size.height to adapt to the changing status bar
}, false)
Es gibt ein Plugin für Cordova, mit dem Sie die IO7-Statusleiste einstellen können. Sie können es [hier] (https://github.com/jota-v/cordova-ios-statusbar) oder [hier] (https://github.com/apache/cordova-labs/tree/plugins/statusbar) bekommen) – TorchMan
@ ryan-miller Welche Version der Telefonlücke verwendest du? –
3.3.0 ist meine aktuelle Version. –