ios - Providing autorotation support to a subview loaded programmatically - iPad -
i have ipad project structured as: - appdelegate - mainwindow - view controller -- view
the view controllers .m file loads view programmatically , positions on screen. view going slid in , out.
i 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 having subview i'm adding here doesnt' seem have correct orientation. project supports landscape, , launches landscape. container view fine, , contains buttons fine well. programmatically loaded view stuck in portrait mode. have provided following auto-rotation code (in loaded view's .m):
- (bool)shouldautorotatetointerfaceorientation:(uiinterfaceorientation)interfaceorientation { // return yes supported orientations return (interfaceorientation == uiinterfaceorientationlandscapeleft || interfaceorientation == uiinterfaceorientationlandscaperight); }
but never gets called.
so, how can programmatically added subview load in landscape , not portrait mode? tia!
the uiview class not receive orientation change messages.
(specially shouldautorotatetointerfaceorientation
method, uiviewcontroller method)
you have manually add method in view inform orientation has changed , should call method in controller shouldautorotatetointerfaceorientation
method.
to have create reference view in controller , take care of memory yourself.
@interface mycontroller : uiviewcontroller { calculatorview *_calculatorview; } @end @implementation mycontroller - (void)viewdidload { [super viewdidload]; cgrect viewrect = cgrectmake(0, 0, 0, 0); //inits _calculatorview without autorelease. released in 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 supported orientations return (interfaceorientation == uiinterfaceorientationlandscapeleft || interfaceorientation == uiinterfaceorientationlandscaperight); } -(void) dealloc { [_calculatorview release]; [super dealloc]; } @end
edit: if calculatorview simple , need change frame after device rotation, think best approach using view's autoresizingmask similar following
_calculatorview = [[calculatorview alloc] initwithframe:viewrect]; _calculatorview.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight;
Comments
Post a Comment