我已将我的应用程序目标更改为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的解决方案。
预先感谢。
尽管这是一个旧帖子,但我最近也面临着这种情况。只需实现协议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")
}
尝试类似:
[[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.
}
我有同样的问题,这是对我有用的东西:
在我的uiviewController中,我必须覆盖ViewWillTransition函数和VOILA。
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的答案很好,只是缺少检测代码。