ユーザーが通話中かテザリング中かを検出するにはどうすればよいですか? 次のような iAd のサブビューがあります。
_UIiAD = [[self appdelegate] UIiAD];
_UIiAD.delegate = self;
[_UIiAD setFrame:CGRectMake(0,470,320,50)];
[self.view addSubview:_UIiAD];\
そして、ユーザーが通話中に間違って設定しますか? どうすればこれを検出できますか?
UIApplicationDelegate
には、この 2 つの方法があります。
// 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
ます。ステータスバーの高さが変わると呼び出されます。そのうちの 1 つは変更前に呼び出され、もう 1 つは変更後に呼び出されます。
変更が発生したときに通知を受けたいViewController
場合、1 つのオプションは通知を送信することです。このような
まず、このプロパティ/変数を追加します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
そして、変更されたときに通知されるようにするには、これをメソッドに追加します。
オブジェクト内
[[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)
}