简体   繁体   English

如何锁定设备上更改方向?

[英]How to lock changing orientation on device?

How to lock changing orientation on device? 如何锁定设备上更改方向?

I tried to use 我尝试使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return UIInterfaceOrientationLandscapeRight;
}

and this 和这个

- (void)viewDidAppear:(BOOL)animated
{
    [self shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
    [super viewDidAppear:animated];
}

but nothing work when i use device. 但是当我使用设备时没有任何效果。 When app runs on simulator it works great. 当应用在模拟器上运行时,效果很好。

simulator have iOS 6 and device 5.1.1. 模拟器具有iOS 6和设备5.1.1。 What i'm doing wrong? 我做错了什么?

Try this: 尝试这个:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

The method returns YES or NO answering the question "should I rotate to this orientation: interfaceOrientation". 该方法返回“是”或“否”,并回答问题“我应该旋转到该方向:interfaceOrientation”。 So you should return YES or NO - UIInterfaceOrientationLandscapeRight is not a BOOL. 因此,您应该返回YES或NO- UIInterfaceOrientationLandscapeRight不是BOOL。 But you can use a comparison to return the BOOL like described above. 但是您可以使用比较来返回布尔值,如上所述。

Open your project, select project file, goto summary section, and set desired interface orientation on 'Supported Interface Orientations' subsection. 打开您的项目,选择项目文件,转到“摘要”部分,然后在“支持的界面方向”子部分中设置所需的界面方向。 Good Luck!(for iOS 6.x) 祝您好运!(适用于iOS 6.x)

In your project .plist file add Supported interface orientations 在您的项目.plist文件中,添加支持的界面方向

and set the item to Landscape(right home button) 并将项目设置为Landscape(主页按钮)

If you want to do it only for 1 viewcontroller, you can do that either by 如果您只想对1个ViewController进行操作,则可以通过以下任一方式进行操作

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

or by

//deprecated
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

and

- (BOOL)shouldAutorotate {
return self.interfaceOrientation == UIInterfaceOrientationPortrait;
}

If you want to set it for the whole app, make sure you change your Info.plist file and add Supported Interface Orientations 如果要为整个应用程序设置它,请确保更改Info.plist文件并添加Supported Interface Orientations

You just add following lines in .m file 您只需在.m文件中添加以下行

#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )
#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

Then you have to add following methods in .m file 然后,您必须在.m文件中添加以下方法

#ifdef IOS_OLDER_THAN_6

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
   return (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
#endif

#ifdef IOS_NEWER_OR_EQUAL_TO_6

- (NSUInteger)supportedInterfaceOrientations
{
   return UIInterfaceOrientationMaskLandscapeRight;
}
#endif

For iOS 6 support include these two methods 对于iOS 6支持包括以下两种方法

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationMaskPortrait;
}

Also kindly note that this will work on xCode 4.5 + 还请注意,这将适用于xCode 4.5 +

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM