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

问题描述 投票:0回答:3

我已经以编程方式创建了一个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];
iphone viewcontroller
3个回答
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];

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

}

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


0
投票

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

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

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
© www.soinside.com 2019 - 2024. All rights reserved.