创建(以编程方式)viewController旋转

时间:2012-04-16 08:29:06

标签: iphone viewcontroller

我已经以编程方式创建了一个viewController,并且我想在旋转设备时强制它旋转。 在简单的viewController中,您可以通过添加新文件以正常方式创建,因此有一个“shouldAutoRotate”方法。 但在我的情况下,我在viewController中创建这个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];

3 个答案:

答案 0 :(得分: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...

}

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

答案 1 :(得分:1)

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

可能不是最好的地方,但是如果你在如何编写FeatureViewController的代码方面苦苦挣扎,它就会是这样的 -

.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];

答案 2 :(得分:0)

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

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

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