Rya*_*ler 15 iphone objective-c statusbar ios cordova
我的问题是:每当iPhone用户正在通话,或者正在使用他或她的手机作为热点时,iOS 7状态栏会被放大,从而将我的Phonegap应用程序的UIWebView推离屏幕底部.放大的状态栏被称为"通话状态栏".见下图:

Stack Overflow答案我试图解决这个问题:
In-Call状态栏如何影响UIViewController的视图大小?(以及如何正确处理)
此外,Phonegap似乎没有任何类型的事件通知我状态栏的更改.听取Phonegap"暂停"事件是没用的,因为1)它已知在iOS中有怪癖,2)它并没有真正涵盖热点案例.
我的Objective-C技能非常小,我只需要在进行必要的4个多小时的谷歌搜索,堆栈溢出,嚎叫等等后提出这样的问题......
Stack Overflow的神,让我觉得你的书呆子愤怒.
根据Jef的建议提出以下解决方案.你想要做的是以下几点:
didChangeStatusBarFrame代表statusBarFrame我已经设置了一个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];
}
Run Code Online (Sandbox Code Playgroud)
使statusBarChange选择器可用
// MainViewController.h
@protocol StatusBarChange <NSObject>
-(void)onStatusbarChange:(NSNotification*)notification;
@end
Run Code Online (Sandbox Code Playgroud)
设置监听器.这得到了origin和size字典从statusBarFrame每当它改变并沿此数据传递的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];
}
Run Code Online (Sandbox Code Playgroud)
所有这些都可以让你倾听window.statusbarchange事件,例如:
// www/js/index.js
window.addEventListener('statusbarchange', function(e){
// Use e.size.height to adapt to the changing status bar
}, false)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7167 次 |
| 最近记录: |