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

これを別の回答として追加する方が簡単です...

おそらくこれを行うのに最適な場所ではありませんが、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 に答える
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 に答える
0

viewController で viewController を作成すると言うときの意味がわかりません。作成する場所は関係ないと思います。また、コードを作成する必要があるため、プログラムで作成することも関係ありません...

VC のコードで、通常の shouldAutorotateToInterfaceOrientation: メソッドを実装するだけです (この例では、横方向のいずれかを許可します) -

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