UIApplicationDidChangeStatusBarOrientationNotification已经弃用,有替代品吗?

7

我将我的应用程序目标更改为IOS 13,以检查我的应用程序中弃用的方法,并且我正在收到以下警告:

'UIApplicationDidChangeStatusBarOrientationNotification'被废弃:自iOS 13.0开始首次弃用-请改用viewWillTransitionToSize:withTransitionCoordinator:代替。
这是我在项目中实现的代码。

    - (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(onChangeStatusBarOrientationNotification:)
                                                     name:UIApplicationDidChangeStatusBarOrientationNotification
                                                   object:nil];
    }
    return self;
}

请为我提供适用于iOS 12和iOS 13的解决方案。

提前感谢。


2
实现 viewWillTransitionToSize:withTransitionCoordinator: 并在其中执行你在 onChangeStatusBarOrientationNotification: 中所做的操作? - Larme
你能给我一个小演示吗? - Mihir Oza
1
  • (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [self onChangeStatusBarOrientationNotification:nil]; }
请将上述代码翻译成中文。
- Larme
3个回答

5
虽然这篇文章是旧的,但我最近也遇到了这种情况。只需实现协议UIContentContainer即可。
@interface MyViewController : NSObject <UIContentContainer>

在实现中实现该方法,并在方法内调用您的函数。

@implementation MyViewController {
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
        [self onChangeStatusBarOrientationNotification];
    }
@end

Swift

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    
    // Put code to be run BEFORE the rotation here... 

    coordinator.animate(alongsideTransition: nil) { _ in
        // Put code to be run after the rotation,
        // or after frame-size change here...
    }
}

其他有用的检查:

if UIDevice.current.orientation.isLandscape {
    print("Landscape")
}
if UIDevice.current.orientation.isFlat {
    print("Flat")
} else {
    print("Portrait")
}

如果它不是视图或没有控制器的视图怎么办?此外,只有在将控制器添加到另一个控制器时才会调用viewWillTransitionToSize(仅添加控制器的视图是不够的)。**请参考我的答案**。 - Top-Master

0

我有同样的问题,这是对我有效的解决方法:

在我的UIViewController中,我必须重写viewWillTransition函数,然后就可以了。

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to:size, with: coordinator)
    let before = self.view.window?.windowScene?.interfaceOrientation
    coordinator.animate(alongsideTransition: nil) { _ in
        let after = self.view.window?.windowScene?.interfaceOrientation
        if before != after {
            // rotation detected, call you rotation handler here
        }
    }

}

来源:https://www.biteinteractive.com/dealing-with-ios-13-deprecations/

Mahmud Ahsan的回答很好,只是缺少检测代码。


也许“if before != after”部分是不必要的,因为回调函数将在每次旋转设备时都被调用(而不仅仅是在用户停止旋转设备后调用一次)。 - Top-Master
在我的情况下,我需要它是因为我只想在方向发生变化时得到通知...我想从这里每个人都可以自行决定接下来该做什么。 - Deian

0

尝试类似以下的代码:

[[NSNotificationCenter defaultCenter] addObserver: self
    selector: @selector(onChangeStatusBarOrientationNotification:)
    name: UIDeviceOrientationDidChangeNotification
    object: nil];

但请注意,它只是起作用,我从未调用以下内容:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

所以,也许它默认是启用的。

Swift 5

NotificationCenter.default.addObserver(self,
    selector: #selector(onLayoutChange),
    name: UIDevice.orientationDidChangeNotification,
    object: nil)

// ...

@objc public func onLayoutChange() {
    // Handle change here.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接