gpt4 book ai didi

iphone - iOS 方向更改不起作用也未收到 UIDeviceOrientationDidChangeNotification

转载 作者:可可西里 更新时间:2023-11-01 05:38:10 26 4
gpt4 key购买 nike

我是 iOS 的新手,我正在拼命尝试让方向更改在我的应用程序中正常工作。研究完后我会这样做:

在我的应用程序中,我有一个管理七个 UIViewController 子类的 NavigationController。

在项目摘要选项卡中,我激活了所有 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*) 通知
{
NSLog(@"orientationChanged");
}


[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self];
分别。

当我在 AppDelegate 的 中执行 beginGeneratingDeviceOrientationNotifications 等时 - (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;

最佳答案

查看您是否在 AppDelegate 的 didFinishLaunchingWithOptions: 方法中设置了 self.window.rootViewController,因为如果您只添加 subview 方向更改通知不会触发到窗口。

关于iphone - iOS 方向更改不起作用也未收到 UIDeviceOrientationDidChangeNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10682663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com