我如何检测用户是否在通话或者共享网络?我有一个像这样的 iAd 子视图:
_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;
[_UIiAD setFrame:CGRectMake(0,470,320,50)];
[self.view addSubview:_UIiAD];\
当用户通话时,它设置错误了吗?我该如何检测到这一点?
UIApplicationDelegate
有这两种方法。
// ObjC
- (void)application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame; // in screen coordinates
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;
// Swift
func application(_ application: UIApplication, willChangeStatusBarFrame newStatusBarFrame: CGRect)
func application(_ application: UIApplication, didChangeStatusBarFrame oldStatusBarFrame: CGRect)
还有通知
。
//ObjC
UIApplicationWillChangeStatusBarFrameNotification
UIApplicationDidChangeStatusBarFrameNotification
// Swift
Notification.Name.UIApplicationWillChangeStatusBarFrame
Notification.Name.UIApplicationDidChangeStatusBarFrame
但是它们没有发布应用程序的启动,所以我不建议使用。
模拟器有一个有用的工具来测试。
硬件->切换呼叫状态栏(⌘Y)
我建议您在AppDelegate
文件中实现这些方法。当状态栏更改其高度时,将调用它们之一。其中一个调用发生在更改之前,另一个调用发生在更改之后。
假设您希望在更改发生时通知您的ViewController
,则可以发送通知。像这样
首先,在AppDelegate
中添加此属性/变量:
// ObjC
@property (assign, nonatomic) CGRect currentStatusBarFrame;
// Swift
var currentStatusBarFrame: CGRect = .zero
然后,实现willChangeStatusBarFrame
// ObjC
- (void) application:(UIApplication *)application willChangeStatusBarFrame:(CGRect)newStatusBarFrame
{
self.currentStatusBarFrame = newStatusBarFrame;
[[NSNotificationCenter defaultCenter] postNotificationName:@"Status Bar Frame Change"
object:self
userInfo:@{@"current status bar frame": [NSValue valueWithCGRect:newStatusBarFrame]}];
}
// Swift
func application(_ application: UIApplication, willChangeStatusBarFrame newStatusBarFrame: CGRect) {
currentStatusBarFrame = newStatusBarFrame
NotificationCenter.default.post(
name: NSNotification.Name(rawValue: "Status Bar Frame Change"),
object: self,
userInfo: ["current status bar frame": newStatusBarFrame])
}
我们已经完成了状态栏框架检查器
的基本部分。接下来,您需要在需要了解状态栏框架的任何ViewController
上实现下一部分。
每当您想要获取状态栏框架时,只需按以下方式进行
// ObjC
[(AppDelegate*)[[UIApplication sharedApplication] delegate] currentStatusBarFrame]
// Swift
(UIApplication.shared.delegate as! AppDelegate).currentStatusBarFrame
要在变化时收到通知,请将以下内容添加到ViewDidLoad
方法中。
使用 ObjC 语言
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(statusBarFrameChanged:)
name:@"Status Bar Frame Change"
object:[[UIApplication sharedApplication] delegate]];
并实现这个方法
- (void) statusBarFrameChanged:(NSNotification*)notification
{
CGRect newFrame = [[notification.userInfo objectForKey:@"current status bar frame"] CGRectValue];
NSLog(@"new height %f", CGRectGetHeight(newFrame));
}
在Swift中
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "Status Bar Frame Change"),
object: nil,
queue: nil) { (note) in
guard let currentStatusBarFrame = note.userInfo?["current status bar frame"] as? CGRect else {return}
print("New Height", currentStatusBarFrame.height)
}
self.tabBarController?.tabBar.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue)))