gpt4 book ai didi

uinavigationcontroller - shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation 在 iOS 7 中无法按预期工作

转载 作者:行者123 更新时间:2023-12-04 05:51:17 24 4
gpt4 key购买 nike

我在尝试阻止某些 View 中的方向时遇到问题,但代码不起作用。

我在每个 View 中都使用这条线:

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return UIInterfaceOrientationMaskPortrait;
} else {
return UIInterfaceOrientationMaskAll;
}
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

它几乎可以在使用 UINavigationController 的 View 中工作,但在使用 UITabBarController 的 View 中我遇到了大问题,因为它接缝没有调用代码。

最佳答案

好的,我解决了,你必须继承 UINavigationController 和 UITabBarController,所以这里是代码:

//cCustomNavigationController.h file

#import <UIKit/UIKit.h>

@interface cCustomNavigationController : UINavigationController <UINavigationControllerDelegate>

@end

//cCustomNavigationController.m file

#import "cCustomNavigationController.h"

@interface cCustomNavigationController ()

@end

@implementation cCustomNavigationController

- (BOOL)shouldAutorotate {
return [self.visibleViewController shouldAutorotate];
}

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

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

@end

//cCustomTabController.h file

#import <UIKit/UIKit.h>

@interface cCustomTabController : UITabBarController <UITabBarControllerDelegate>

@end

//cCustomTabController.m file

#import "cCustomTabController.h"

@interface cCustomTabController ()

@end

@implementation cCustomTabController

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

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

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

@end

现在你只需要在你需要的地方使用这个类创建你的 TabBarController 或你的 NavigationController,即
//For the UINavigationController
UINavigationController *navigationController = [[cCustomNavigationController alloc] init];

//For the UITabBarController
UITabBarController *tabController = [[cCustomTabController alloc] init];

我希望这对你们有帮助。

关于uinavigationcontroller - shouldAutorotate、supportedInterfaceOrientations 和 preferredInterfaceOrientationForPresentation 在 iOS 7 中无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20355089/

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