19

如何检测用户是否正在通话或网络共享?我有一个这样的 iAd 子视图:

_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;

[_UIiAD setFrame:CGRectMake(0,470,320,50)];
[self.view addSubview:_UIiAD];\

并且在用户通话时设置错误?我如何检测到这一点?

4

2 回答 2

34

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)

而且也有Notifications

//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])
}

我们已经完成了Status Bar Frame Checker. 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));
}

在斯威夫特

    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)
    }
于 2014-05-24T05:01:03.330 回答
1

我相信您有状态栏问题,这使您向下查看。您可以使用下面的代码来解决这个问题

迅速

self.tabBarController?.tabBar.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue)))

它解决了我在用户通话时由状态栏引起的问题。目标 C 参考点击这里

于 2018-05-16T09:42:35.617 回答