开发者

Providing autorotation support to a subview loaded programmatically - iPad

I have an iPad project structured as: - AppDelegate - MainWindow - View Controller -- View

The View Controllers .m file loads another view programmatically and positions it on the screen. This view is going to be slid in and out.

I do this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect  viewRect = CGRectMake(0, 0, 0, 0);
    CalculatorView *v = [[[CalculatorView alloc] 
                                    initWithFrame:viewRect] autorelease];
    [self.view.window addSubview:v];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    v.view.frame   = CGRectMake(0, 0, 460, 320);
    [UIView commitAnimations];
}

The issue that I am having is that the subview I'm adding here doesnt' seem to have the correct orientation. The project supports ONLY landscape, and launches to landscape. The container view is fine, and it contains some buttons which are fine as well. However this programmatically loaded view is stuck in Portrait mode. I have provided the following auto-rotation code (In the loaded view's .m):

开发者_Python百科
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

But it never gets called.

So, how can I get the programmatically added subview to load in landscape and NOT portrait mode? TIA!


The UIView class does not receive orientation change messages.

(specially the shouldAutorotateToInterfaceOrientation method, which is a UIViewController Method)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW23

You will have to manually add a method in your view to inform it that the orientation has changed and you should call this method in your controller shouldAutorotateToInterfaceOrientation method.

To do that you will have to create a reference to you view in your controller and take care of the memory yourself.

@interface MyController : UIViewController {

   CalculatorView *_calculatorView;

}

@end

@implementation MyController


- (void)viewDidLoad
{
    [super viewDidLoad];

    CGRect  viewRect = CGRectMake(0, 0, 0, 0);

   //inits _calculatorView without the autorelease. Will be released in the dealloc method
    _calculatorView = [[CalculatorView alloc] 
                                    initWithFrame:viewRect];
    [self.view.window addSubview:v];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    _calculatorView.view.frame   = CGRectMake(0, 0, 460, 320);
    [UIView commitAnimations];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   //calls custom interface orientation method
   [_calculatorView MyInterfaceChangedCustomMethod:interfaceOrientation];

    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

-(void) dealloc { 

   [_calculatorView release];
   [super dealloc];
}



@end

EDIT: If you CalculatorView is simple and all you need is to change its frame properly after the device rotation, I think the best approach would be using you view's autoresizingMask similar to the following

_calculatorView = [[CalculatorView alloc] 
                                    initWithFrame:viewRect];
_calculatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜