0

我以编程方式制作了一个 viewController,我想在设备旋转时强制它旋转。在简单的 viewController 中,您通过添加一个新文件使用常规方式创建,因此有一个“shouldAutoRotate”方法.. 但在我的情况下,它是不同的,特别是我在 viewController 中创建这个 viewController!

而且我不想创建一个新的 viewController。

这是我用来创建视图控制器的代码

UIViewController *featuresViewController = [[UIViewController alloc]initWithNibName:@"featrures" bundle:nil];
[featuresViewController setView:[[UIView alloc]initWithFrame:CGRectMake(10, 10, 380, 450 )]];
[featuresViewController.view setBackgroundColor:[UIColor clearColor]];

featuresViewController.modalTransitionStyle = UIModalTransitionStylePartialCurl;      

featuresViewController.modalPresentationStyle = UIModalPresentationPageSheet;
[self presentModalViewController:featuresViewController animated:YES];
4

3 回答 3

1

在您的应用程序中添加以下代码...

//在@implementation之前添加以下行

@interface UIDevice (UndocumentedFeatures) 
 -(void)setOrientation:(UIInterfaceOrientation)orientation animated:(BOOL)animated;
-(void)setOrientation:(UIInterfaceOrientation)orientation;
@end

//here you can use following code in any method..i just used here in sample method... 
-(IBAction)rotateviewprogramatically
{
   **//you can also add this in Viewdidload or Viewwillappear...it will work...**

   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
   //or
   [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
 }

//更改以下代码....在您的代码中添加以下方法...我检查了它是否正常工作...

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return (interfaceOrientation == UIInterfaceOrientationPortrait);// don't use this
return YES; // use this...

}

希望,它会帮助你......冷静

于 2012-04-16T08:41:54.520 回答
1

更容易将此添加为另一个答案...

可能不是执行此操作的最佳位置,但如果您正在为如何编写 FeaturesViewController 代码而苦苦挣扎,它将是这样的 -

。H -

#import <UIKit/UIKit.h>

@interface FeaturesViewController : UIViewController {
    // ivar declarations...
}

@end

.m -

#import "FeaturesViewController.h"

@implementation FeaturesViewController

-(id)init {

    self = [super initWithNibName:@"FeaturesViewController" bundle:nil];

    if (self) {
        // other init stuff
    }

    return self;

}


-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // return YES for whatever orientations you want
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

然后在你的主 VC 中,像这样呈现它 -

FeaturesViewController *featuresViewController = [[[FeaturesViewController alloc] init] autorelease];

featuresViewController.modalTransitionStyle=UIModalTransitionStylePartialCurl;      
featuresViewController.modalPresentationStyle=UIModalPresentationPageSheet;

[self presentViewController:featuresViewController animated:YES completion:nil];
于 2012-04-16T09:49:54.840 回答
0

当你说你在 viewController 中创建 viewController 时,我不明白你的意思——我认为你在哪里创建它是无关紧要的。您以编程方式创建它也无关紧要,因为您仍然必须编写代码......

在您的 VC 代码中,只需实现正常的 shouldAutorotateToInterfaceOrientation: 方法(此示例允许横向中的任何一种) -

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
于 2012-04-16T08:59:06.283 回答