ios - 将旋转视图控制器更改为单个通用视图控制器目标 c
问题描述
好的,所以我正在这里开发一个objective-c ios 应用程序,其中我有多个纵向视图控制器。但是,我不希望这些视图控制器中的任何一个以横向显示,而是我想要发生的是单个横向视图控制器在设备旋转到横向时继续插入,并在旋转时再次传递到特定视图控制器回到肖像。这不仅仅是调整现有视图大小的情况,我该怎么做呢?
解决方案
我没有尝试过,但我会这样开始:听方向变化......
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
更改为横向后,呈现横向 vc。更改为肖像后,将其关闭...
- (void)orientationDidChange:(NSNotification *)notification {
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(orientation) && self.presentedViewController) {
[self.presentedViewController dismissViewControllerAnimated:YES completion:nil];
} else if (UIInterfaceOrientationIsLandscape(orientation) && !self.presentedViewController) {
MyLandscapeVC *landscapeVC = [[MyLandscapeVC alloc] initWithNibName:@"MyLandscapeVC" bundle:nil]; // or however you make this
[self presentViewController:landscapeVC animated:YES completion:nil];
}
}
额外的检查self.presentedViewController
是防止系统在快速来回旋转期间出现任何马虎,因此我们永远不会堆叠超过一个景观 vcs。这可能是不必要的。
这可以使用 UIViewController 上的类类别将其隔离在一个文件中,就像这样......
// in UIViewController+Rotations.h
@interface UIViewController (Rotations)
- (void)observeRotations;
@end
// in UIViewController+Rotations.m
#import "UIViewController+Rotations.h"
@implementation UIViewController (Rotations)
- (void)observeRotations {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)orientationDidChange:(NSNotification *)notification {
// etc., from above
}
@end
只需在视图控制器中导入 UIViewController+Rotations.h,并在生命周期早期(可能是 viewWillAppear)调用[self observeRotations];
推荐阅读
- sql - BigQuery:使用 DML 原子地替换日期分区
- git - 如何仅合并来自另一个分支的一些文件
- android - react-native-udp Sochets.createSocket 未定义对象
- excel - 整个工作簿的 SUMIF
- java - Javadoc @link 到 Kotlin 类
- r - R闪亮的checkboxGroup在地图上绘制数据
- laravel - Laravel 5.4 Route [login] 未定义
- scala - 什么可以在尾递归功能中炸毁我的笔记本电脑?
- f# - 是否可以重复一个数组?
- spring - 为什么@MockBean 和@InjectMocks 会导致BeanCreationException?