问题描述
整个星期我一直在寻找解决我的独特问题的方法,但是对我来说没有任何效果。我有一个TabBarController有6个选项卡。 iOS自动创建更多选项卡,其中包含我遇到困难的视图控制器仪表板。 仪表板视图控制器充当页面控制器,有3页。
当我按仪表板项目时,我希望屏幕方向从纵向更改去景观。我通过无数其他SO帖子了解到旋转是从父TabBarController控制的。我还了解到更多选项卡实际上是一个导航控制器,它会为混音添加更多复杂功能。
重复并直接,当我按下时TabBarController的更多选项卡中的仪表板选项卡,我希望屏幕将其方向从纵向更改为横向。
是的,我已经尝试过这样做: [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@orientation];
并且它不起作用。
我也尝试覆盖TabBarController和MoreNavigationController中与旋转和方向以及子视图有关的所有函数。我将在下面发布我的TabBarController。任何帮助将不胜感激,谢谢。
TabBarViewController.h:
@interface TabBarViewController:UITabBarController< UINavigationControllerDelegate> {}
@end
TabBarViewController.m:
@implementation TabBarViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.moreNavigationController.delegate = self;
}
- (BOOL)shouldAutorotate
{
返回YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if([self.selectedViewController isKindOfClass:[Dashboard class]])
{
返回UIInterfaceOrientationMaskLandscape;
}
返回UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
{
if([tabBarController.selectedViewController isKindOfClass:[Dashboard class]])
{
返回UIInterfaceOrientationMaskLandscape;
}
返回UIInterfaceOrientationMaskAll;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@tabBarController didSelectViewController =%lu,(unsigned long)tabBarController.selectedIndex);
}
- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
{
if([navigationController.topViewController isKindOfClass:[Dashboard class]]
|| [navigationController.topViewController isKindOfClass:[ViewController1 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController2 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController3 class] ])
{
返回UIInterfaceOrientationMaskLandscape;
}
返回UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController
{
NSLog(@navigationControllerPreferredInterfaceOrientationForPresentation);
if([navigationController.topViewController isKindOfClass:[Dashboard class]]
|| [navigationController.topViewController isKindOfClass:[ViewController1 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController2 class] ]
|| [navigationController.topViewController isKindOfClass:[ViewController3 class]])
{
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
返回UIInterfaceOrientationPortrait | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if([viewController isKindOfClass:[Dashboard class]])
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeLeft] forKey:@orientation];
}
}
@end
更改UITabBarController的方向
虽然有很多可能和尝试这个对于这个问题,主要问题在于每个 UINavigationController
的精心设计,对 supportedInterfaceOrientations的适当响应
在 UITabBarController
中,以及一些强制执行所需方向的代码。
很有可能,这可能只是一个iOS错误,因为一旦旋转, UINavBarController
和所有嵌入的 UINavigationController
表现正常。
以下声明不正确。标签栏控制器旋转。
每个 UINavigationController的设置方向
横向示例:
class HorizontalNavigationController:UINavigationController {
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
return .landscape
}
}
肖像示例:
class VerticalNavigationController:UINavigationController {
override var supportedInterfaceOrientations:UIInterfaceOrientationMask {
return。 portrait
}
}
推迟 TabBarController
所选视图控制器的方向
class TabBarController:UITabBarController {
覆盖var supportedInterfaceOrientations:UIInterfaceOrientationMask {
if let selectedViewController = selectedViewController {
return selectedViewController.supportedInterfaceOrientations
}
return .allButUpsideDown
}
}
强制更改界面方向
这是 UITabBarController
的争议方面。不知何故,方向改变不会像在 UINavigationController
中那样发生,并且需要轻推。
待办事项因此,使用最少量的代码,使用Object并将其子类化为 TabBarControllerDelegate
。您可以
►在以及有关 Swift Recipes 。
All week I have been searching SO for a solution to my unique problem, but nothing has worked for me yet. I have a TabBarController that has 6 tabs. iOS automatically creates the "More" tab, which contains the view controller,"Dashboard", that I am having difficulty with. The "Dashboard" view controller is acting as a page controller that has 3 pages.
When I press the "Dashboard" item, I want the screen orientation to change from Portrait to Landscape. I've learned through countless other SO posts that rotation is controlled from the parent TabBarController. I have also learned that the "More" tab is actually a navigation controller, which adds some more complication to the mix.
To reiterate and be direct, when I press the "Dashboard" tab in the TabBarController's "More" tab, I want the screen to change its orientation from Portrait to Landscape.
And YES, I have already tried this: [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]forKey:@"orientation"];
and it didn't work.
I've also tried overriding every function in the TabBarController and MoreNavigationController that has to do with rotation and orientation, as well as in the child views. I will post my TabBarController below. Any help will be appreciated, thanks.
TabBarViewController.h:
@interface TabBarViewController : UITabBarController<UINavigationControllerDelegate>{}
@end
TabBarViewController.m:
@implementation TabBarViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.moreNavigationController.delegate = self;
}
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
if([self.selectedViewController isKindOfClass:[Dashboard class]])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return YES;
}
- (NSUInteger)tabBarControllerSupportedInterfaceOrientations:(UITabBarController *)tabBarController
{
if([tabBarController.selectedViewController isKindOfClass:[Dashboard class]])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAll;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"tabBarController didSelectViewController = %lu", (unsigned long)tabBarController.selectedIndex);
}
- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController
{
if([navigationController.topViewController isKindOfClass:[Dashboard class]]
|| [navigationController.topViewController isKindOfClass:[ViewController1 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController2 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController3 class]])
{
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController
{
NSLog(@"navigationControllerPreferredInterfaceOrientationForPresentation");
if([navigationController.topViewController isKindOfClass:[Dashboard class]]
|| [navigationController.topViewController isKindOfClass:[ViewController1 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController2 class]]
|| [navigationController.topViewController isKindOfClass:[ViewController3 class]])
{
return UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight;
}
return UIInterfaceOrientationPortrait|UIInterfaceOrientationMaskLandscapeRight|UIInterfaceOrientationMaskLandscapeLeft;
}
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
if([viewController isKindOfClass:[Dashboard class]])
{
[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]forKey:@"orientation"];
}
}
@end
Changing the orientation of a UITabBarController
While there are dozens of maybe and try this for this problem, the main issue reside in the careful design of each UINavigationController
, a proper response to supportedInterfaceOrientations
in the UITabBarController
, and some code to enforce the desired orientation.
In all likelihood, this may just be an iOS bug, since once rotated, the UINavBarController
and all embedded UINavigationController
behave correctly.
The following statement is not true. Tab bar controllers do rotate.
Setup orientation for each UINavigationController
Landscape example:
class HorizontalNavigationController: UINavigationController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .landscape
}
}
Portrait example:
class VerticalNavigationController: UINavigationController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
return .portrait
}
}
Defer TabBarController
orientation to selected view controller
class TabBarController: UITabBarController {
override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
if let selectedViewController = selectedViewController {
return selectedViewController.supportedInterfaceOrientations
}
return .allButUpsideDown
}
}
Enforce the interface orientation change
This is the contentious aspect of UITabBarController
. Somehow, the orientation change does not happen like it would in a UINavigationController
, and needs a nudge.
To do so with the fewest amount of code, use an Object and subclass it into a TabBarControllerDelegate
. You can do most of the work in Interface Builder.
class TabBarControllerDelegate: NSObject, UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
let orientedViewController = UIViewController()
tabBarController.present(orientedViewController, animated: false) { () -> Void in
tabBarController.dismiss(animated: false, completion: { () -> Void in
})
}
}
}
Storyboard Structure
Using a Storyboard
helps visualize the structure of the application as well as the relationship between all classes.
► Find this solution on GitHub and additional details on Swift Recipes.
这篇关于TabBarController旋转问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!