我正在写一个应用程序,如果用户在打电话时正在看应用程序,我需要改变视图。

我实现了以下方法:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"viewWillAppear:");
    _sv.frame = CGRectMake(0.0, 0.0, 320.0, self.view.bounds.size.height);
}

但当应用程序返回前台时,它没有被调用。

我知道我可以实现:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarFrameChanged:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];

但我不想这么做我更愿意把我所有的布局信息放在viewWillAppear:方法中,并让它处理所有可能的场景。

我甚至尝试调用viewWillAppear: from applicationWillEnterForeground:,但我似乎无法确定在那一点上哪个是当前视图控制器。

有人知道正确的处理方法吗?我肯定我错过了一个显而易见的解决方案。


当前回答

Swift 4.2 / 5

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground),
                                           name: Notification.Name.UIApplication.willEnterForegroundNotification,
                                           object: nil)
}

@objc func willEnterForeground() {
   // do what's needed
}

其他回答

只是想让它尽可能简单,请参阅下面的代码:

- (void)viewDidLoad
{
   [self appWillEnterForeground]; //register For Application Will enterForeground
}


- (id)appWillEnterForeground{ //Application will enter foreground.

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(allFunctions)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object:nil];
    return self;
}


-(void) allFunctions{ //call any functions that need to be run when application will enter foreground 
    NSLog(@"calling all functions...application just came back from foreground");


}

Swift 4.2 / 5

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground),
                                           name: Notification.Name.UIApplication.willEnterForegroundNotification,
                                           object: nil)
}

@objc func willEnterForeground() {
   // do what's needed
}

使用ViewController的viewDidLoad:方法中的通知中心来调用一个方法,并从那里做你应该在viewWillAppear:方法中做的事情。直接调用viewWillAppear不是一个好的选择。

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");

    [[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(applicationIsActive:) 
        name:UIApplicationDidBecomeActiveNotification 
        object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self 
        selector:@selector(applicationEnteredForeground:) 
        name:UIApplicationWillEnterForegroundNotification
        object:nil];
}

- (void)applicationIsActive:(NSNotification *)notification {
    NSLog(@"Application Did Become Active");
}

- (void)applicationEnteredForeground:(NSNotification *)notification {
    NSLog(@"Application Entered Foreground");
}

斯威夫特

简短的回答

使用NotificationCenter观察者,而不是viewWillAppear。

override func viewDidLoad() {
    super.viewDidLoad()

    // set observer for UIApplication.willEnterForegroundNotification
    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

}

// my selector that was defined above
@objc func willEnterForeground() {
    // do stuff
}

长回答

要找出应用程序何时从后台返回,使用NotificationCenter观察者而不是viewWillAppear。下面是一个示例项目,它显示了何时发生哪些事件。(这是对Objective-C答案的改编。)

import UIKit
class ViewController: UIViewController {

    // MARK: - Overrides

    override func viewDidLoad() {
        super.viewDidLoad()
        print("view did load")

        // add notification observers
        NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

    }

    override func viewWillAppear(_ animated: Bool) {
        print("view will appear")
    }

    override func viewDidAppear(_ animated: Bool) {
        print("view did appear")
    }

    // MARK: - Notification oberserver methods

    @objc func didBecomeActive() {
        print("did become active")
    }

    @objc func willEnterForeground() {
        print("will enter foreground")
    }

}

在第一次启动应用程序时,输出顺序是:

view did load
view will appear
did become active
view did appear

在按下home键,然后将应用程序带回前台后,输出顺序为:

will enter foreground
did become active 

如果你最初尝试使用viewWillAppear,那么UIApplication。willEnterForegroundNotification可能是你想要的。

Note

从iOS 9及以后版本开始,你不需要移除观察者。文件说明:

如果您的应用程序的目标是iOS 9.0及以上版本或macOS 10.11及以上版本,您可以使用 不需要在观察器的dealloc方法中取消注册。

在我看来,viewWillAppear:animated:是iOS sdk中最令人困惑的方法之一,在这种情况下,即应用程序切换时,它永远不会被调用。该方法只根据视图控制器的视图和应用程序窗口之间的关系被调用,也就是说,只有当视图控制器的视图出现在应用程序的窗口上,而不是屏幕上时,消息才会被发送到视图控制器。

当你的应用程序进入后台时,显然应用程序窗口的最顶层视图对用户来说不再可见。然而,在应用程序窗口的透视图中,它们仍然是最顶层的视图,因此它们并没有从窗口中消失。相反,这些视图消失是因为应用程序窗口消失了。他们没有消失,因为他们从窗口消失了。

因此,当用户切换回您的应用程序时,他们显然似乎出现在屏幕上,因为窗口再次出现。但从窗户的角度来看,它们根本没有消失。因此视图控制器永远不会得到viewWillAppear:animated消息。