强制横竖屏
设置横竖屏支持模式:
1.项目配置:General -> Deployment Info -> Device Orientation
2.AppDelegate中设置:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
强制设置屏幕模式:
1.KVC:
NSNumber *orientationTarget = [NSNumber numberWithInt:interfaceOrientation];// 设置成想要的方向
[[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];
2.NSInvocation
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[UIDevice currentDevice]];
int val = orientation;
// 从2开始是因为0 1 两个参数已经被selector和target占用
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
当每次设置UIDevice 的orientation属性后,都会调用AppDelegate:supportedInterfaceOrientationsForWindow方法,所以这个方法也需要动态的去支持不同的方向,以便达到想要的效果。
拓展知识点:
当我们设置横屏后,点击左上角的返回按钮的事件可能用户的需求是恢复竖屏
获取到navigationItem的点击事件:UINavigationBar 的代理方法UIBarPositioningDelegate
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPushItem:(UINavigationItem *)item; // called to push. return NO not to.
- (void)navigationBar:(UINavigationBar *)navigationBar didPushItem:(UINavigationItem *)item; // called at end of animation of push or immediately if not animated
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item; // same as push methods
- (void)navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item;
网友评论