gpt4 book ai didi

ios - 无法停止设备旋转

转载 作者:行者123 更新时间:2023-11-29 10:43:48 30 4
gpt4 key购买 nike

我已经发布了一个问题 here .其中一位哥们回复并给出了解决方案。虽然该解决方案适用于少数 View Controller ,但在 View 中它不起作用。当我在选项卡栏项目上输入具有 TabController + 导航 Controller 的 View Controller 时,代码不起作用。并且 View 能够旋转。

我为 iOS 6.1 使用了以下代码

//For iOS6
- (BOOL)shouldAutorotate {
return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}

我也必须在 iOS 7 中实现相同的功能。请帮忙

最佳答案

shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation

如果上述方法位于 navigationcontroller 的任何 tabbarcontroller 中,则它们不会被 viewcontroller 调用。如果这些方法在 tabbarcontroller 或导航 Controller 中声明,那么它们将被调用。

为了解决这个问题,创建一个类FixedOrientationTab,它是UITabBarController的子类,还有一个导航类OrientationEnabledNavigation,它是UITabBarController的子类UINavigationController。然后在 FixedOrientationTabOrientationEnabledNavigation 中实现了 shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation 方法。

OrientationEnabledNavigation.h

#import <UIKit/UIKit.h>

@interface OrientationEnabledNavigation : UINavigationController

@end

OrientationEnabledNavigation.m

#import "OrientationEnabledNavigation.h"

@interface OrientationEnabledNavigation ()

@end

@implementation OrientationEnabledNavigation

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
// return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

FixedOrientationTab.h

#import <UIKit/UIKit.h>

@interface FixedOrientationTab : UITabBarController

@end

FixedOrientationTab.m

#import "FixedOrientationTab.h"

@interface FixedOrientationTab ()

@end

@implementation FixedOrientationTab

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}

- (BOOL)shouldAutorotate
{
return [self.selectedViewController shouldAutorotate];
// return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
return [self.selectedViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

然后,如果您想在项目中使用导航 Controller ,则使用 OrientationEnabledNavigation 和标签栏 FixedOrientationTab。之后,如果您在 viewcontroller 中实现 shouldAutorotate、supportedInterfaceOrientations、preferredInterfaceOrientationForPresentation 这些方法,那么它们将被调用。

希望这会有所帮助..:)

编辑正如 Jeev 指出的另一种方法是在应用程序委托(delegate)的开头添加一些代码:

对于 UITabBarController

@implementation UITabBarController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
if ([self.selectedViewController respondsToSelector:@selector(shouldAutorotate)]) {
return [self.selectedViewController shouldAutorotate];
}
else {
return YES;
}
}

- (NSUInteger)supportedInterfaceOrientations {
if ([self.selectedViewController respondsToSelector:@selector(supportedInterfaceOrientations)]) {
return [self.selectedViewController supportedInterfaceOrientations];
}
else {
return UIInterfaceOrientationMaskAll;
}
}

@end

对于 UINavigationController

@implementation UINavigationController (AutoRotationForwarding)

-(BOOL)shouldAutorotate
{
if ([self.topViewController respondsToSelector:@selector(shouldAutorotate)]) {
return [self.topViewController shouldAutorotate];
}
else {
return YES;
}
}

-(NSUInteger) supportedInterfaceOrientations {
if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
{
return [self.topViewController supportedInterfaceOrientations];
}
return UIInterfaceOrientationMaskPortrait;
}

@end

关于ios - 无法停止设备旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179515/

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