我对 iOS 很陌生,我正在拼命地尝试在我的应用程序中进行方向更改。经过研究,这是我所做的:
在我的应用程序中,我有一个 NavigationController 管理七个 UIViewController 子类。
在项目摘要选项卡中,我激活了所有 4 个设备方向。
每个 UIViewcontroller 子类都有一个 xib 文件,所有 xib 文件都激活了“autoresize subviews”。
UIViewController 子类都有:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait ||
interfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
interfaceOrientation == UIInterfaceOrientationLandscapeRight );
}
他们也都有:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
和:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
用 NSLog(...) 语句实现(从不打印,调试器也从不进入这些方法)。
我也试图使用:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(@"will receive orientation notifications: %@", getOrientationUpdates?@"YES":@"NO");
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
和
-(void)orientationChanged: (NSNotification*) notification
{
NSLog(@"orientationChanged");
}
和
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
分别。
当我beginGeneratingDeviceOrientationNotifications
在 AppDelegate 的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中执行等操作时,orientationChanged: 在启动时被调用一次,但再也不会旋转设备,但当我在 UIViewController 子类之一中执行此操作时,它永远不会被调用!
到目前为止,我想要实现的只是获取方向通知以旋转 UIImageView 和 UIImage(在不同方向上没有任何布局更改)。
UIDeviceOrientation o = [UIDevice currentDevice].orientation;
总是返回UIDeviceOrientationPortrait
可能是我错过了文档或 stackoverflow 中的某些内容,但我显然无法弄清楚我需要做什么/添加才能让它在我的设置中工作。我对stackoverflow也很陌生,所以我希望我的帖子没有违反任何平台约定。
非常感谢任何帮助/提示。太感谢了!
编辑:getOrientationUpdates 总是 YES,这对我来说很奇怪,因为当我旋转它时,通知回调选择器永远不会被调用!
编辑:在我的 AppDelegate 的 didFinishLaunchingWithOptions 中,我正在做:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.regScreenController = [[RegistrationScreenController alloc] initWithNibName:@"RegistrationScreenController" bundle:nil];
navCtrl = [[UINavigationController alloc]initWithRootViewController:self.regScreenController];
[navCtrl setNavigationBarHidden:YES];
self.window.rootViewController = self.regScreenController;
[self.window addSubview:navCtrl.view];
[self.window makeKeyAndVisible];
return YES;