如何在横向模式下打开视图控制器?
Posted
技术标签:
【中文标题】如何在横向模式下打开视图控制器?【英文标题】:How to open a view controller in landscape mode? 【发布时间】:2014-04-21 08:55:21 【问题描述】:我有一个表格视图,当在表格中点击单元格时,我正在推动另一个视图控制器 如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil];
PriceChart *vc = [storyboard instantiateViewControllerWithIdentifier:@"pricechart"];
[self.navigationController pushViewController:vc animated:YES];
现在我的问题是:我要展示的视图控制器应该是横向模式,但它是纵向模式。
另一个问题是如何在点击不同的单元格时打开不同的视图控制器。我尝试使用indexpath.row
,但有没有其他方法使用storyboard
?
【问题讨论】:
【参考方案1】:shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
如果UIViewController
在任何UINavigationController
内,则不会调用上述方法。如果这些方法在UINavigationController
中声明,那么它们将被调用。
为了解决这个问题,创建一个类,我们称之为OrientationEnabledNavigation
,它是UINavigationController
的子类。然后在OrientationEnabledNavigation
内部实现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
如果您想在项目中使用导航控制器,请使用OrientationEnabledNavigation
。之后,如果您在视图控制器中实现shouldAutorotate, supportedInterfaceOrientations, preferredInterfaceOrientationForPresentation
这些方法,那么它们将被调用。
在你的视图控制器中实现这些:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);
-(BOOL)shouldAutorotate
return NO;
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskPortrait;
另一种方法是在您的应用委托的开头添加一些代码:
对于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
希望这会有所帮助.. :)
【讨论】:
【参考方案2】:这就是你可以强制横向的方式
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return (interfaceOrientation == UIInterfaceOrientationPortrait);
-(BOOL)shouldAutorotate
return NO;
-(NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskPortrait;
【讨论】:
【参考方案3】:在横向模式的班级中:
- (NSUInteger)supportedInterfaceOrientations
// return orientation that you want
//return UIInterfaceOrientationMaskPortrait|UIInterfaceOrientationMaskPortraitUpsideDown;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
return YES;
//return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
如果你使用 UINavigationController,你应该从 UINavigationController 继承你的 navigationController 并实现
- (BOOL)shouldAutorotate
return YES;
- (NSUInteger)supportedInterfaceOrientations
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
return [[self.viewControllers lastObject] shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
【讨论】:
【参考方案4】:试试这个.. 这个演示解决了我的问题...
https://github.com/alloy/ForceOrientationTest
【讨论】:
【参考方案5】:您可以按照here 和该问题中的其他答案的说明强制刷新方向。
至于第二个问题,只需使用静态单元格或动态原型单元格填充 UITableViewController
。然后控制+从单元格拖动到情节提要中的另一个视图控制器。您将拥有与按钮显示相同的 Push/Modal/Custom 操作。
【讨论】:
我试过了,但通过这种方式,我可以为所有单元格显示相同的控制器。 你是说你能推还是不能推?以上是关于如何在横向模式下打开视图控制器?的主要内容,如果未能解决你的问题,请参考以下文章
在拆分视图中,当应用程序以横向模式启动时如何将视图调整到屏幕