如何检测用户是在通话还是网络共享?我有这样的iAd子视图:
_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;
[_UIiAD setFrame:CGRectMake(0,470,320,50)];
[self.view addSubview:_UIiAD];\
用户在通话时设置错误了?我如何检测到这一点?
答案 0 :(得分:33)
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));
}
在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)
}
答案 1 :(得分:1)
我相信你有状态栏问题,让你看向下移。您可以使用以下代码来解决该问题
快速
self.tabBarController?.tabBar.autoresizingMask = UIViewAutoresizing(rawValue: UIViewAutoresizing.RawValue(UInt8(UIViewAutoresizing.flexibleWidth.rawValue) | UInt8(UIViewAutoresizing.flexibleTopMargin.rawValue)))
它解决了用户在通话时由状态栏引起的问题。对于客观C参考click here