gpt4 book ai didi

iphone - UITabBarController 与 viewControllers 利用不同的方向?

转载 作者:行者123 更新时间:2023-12-03 21:23:08 28 4
gpt4 key购买 nike

我可以看到这是困扰很多人的事情:/

我有一个 UITabBarController,它有 4 个 viewController,全部都是 UINavigationController 类型。导航 Controller 之一将 View Controller 推送到其堆栈上,该 View Controller 应以横向模式/方向呈现。

viewController 是一个图表,它是应用程序中景观唯一有意义的地方。 (当出现 UITabBar 时,我会隐藏它,以免让用户相信这在任何地方都有效)

为了使 UITabBarController 正确响应方向的变化,其所有 viewController 需要从委托(delegate)方法返回相同的值:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

因此,为了适应这种行为,我在属于 UITabBarController 的所有 viewController 中实现了此方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL canRotate = [defaults boolForKey:@"can_rotate"];

return canRotate;
}

现在的“技巧”是,当我的 can-be-landscape viewController 被推送时,我会这样做:

- (void) viewWillAppear:(BOOL)animated {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"can_rotate"];
[defaults synchronize];

}

当它弹出时,我这样做:

- (void) viewWillDisappear:(BOOL)animated {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:NO forKey:@"can_rotate"];
[defaults synchronize];

}

这个效果非常好。当 viewController 位于堆栈上时,我可以旋转设备, View 也会随之旋转。

但是问题是,如果用户在横向模式下点击导航栏上的“后退”按钮,从而将 viewController 弹出到前一个 viewController,那么这个“旧”viewController 当然也是处于横向模式。更糟糕的是,因为我将 BOOL 设置为 NO,所以当我将设备定向为纵向模式时,这个“旧”viewController 无法旋转回去。

有没有办法更新所有内容,以便当我弹出可横向模态视图 Controller 时,我的其他 View Controller 都不会处于横向模式?

我有点担心,如果这可以从横向到纵向完成,那么从纵向到横向也应该是可能的,从而使我的“黑客”变得不必要..但如果不能,那么我又回到了原点:/

希望我很接近并且有人可以帮助我到达那里,谢谢:)

最佳答案

我想我可以简化整个事情......

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
return NO;
}<p></p>

<p>-(void) viewDidAppear:(BOOL)animated {
[UIView beginAnimations:@"View Flip" context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];</p>

<pre><code> self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(90.0*0.0174532925);
self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
self.view.center = CGPointMake(160.0f, 240.0f);
</code></pre>

<p>// [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationLandscapeRight;</p>

<pre><code>[UIView commitAnimations];
[super viewDidAppear:animated];
</code></pre>

<p>}</p>

<p></p>

不会自动旋转任何内容,但您可以手动旋转要移动的 View 。如果您不需要 View 中的键盘支持,则无需更改 statusBarOrientation。

如果您并不总是希望旋转该 View ,那么您会遇到一个稍微困难的问题 - 您可能需要检查加速度计值以找出手机的方向(或者可能暗示手机的方向)已通过调用 shouldAutotateToInterfaceOrientation 进行转换,然后在需要时调用转换代码。

关于iphone - UITabBarController 与 viewControllers 利用不同的方向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3009206/

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