2013-02-05 2 views
0

몇 가지 사용자 지정 단추가 추가 된 간단한 viewcontroller가 있습니다.viewcontroller not rotating

-(void)viewWillLayoutSubviews { 

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
{ 
    NSLog(@"Detected IPAD"); 
    if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) 
    { 
     NSLog(@"Orientation is PORTAIT"); 
    UIImage *serviceTask = [UIImage imageNamed:@"main_l_serviceorder.png"]; 

    btnServiceOrder = [UIButton buttonWithType:UIButtonTypeCustom]; 
    btnServiceOrder.frame = CGRectMake(143.0, 37.0, 226.0, 200.0); 
    [btnServiceOrder setBackgroundImage:serviceTask forState:UIControlStateNormal]; 
    [btnServiceOrder addTarget:self action:@selector(gotoServiceOrders:) forControlEvents:UIControlEventTouchUpInside]; 
    [self.view addSubview:btnServiceOrder]; 

    //similar codes goes here.. 
    } 
    else 
    { 
    NSLog(@"Orientation is LANDSCAPE"); 
    //similar codes goes here.. 
    } 
    } 
} 

은 내가 감가 상각 iOS6의 shouldAutorotateToInterfaceOrientation 같이 다음과 같은 추가했습니다.,

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
    NSLog(@"Rotated!."); 
} 

- (BOOL)shouldAutorotate { 
return YES; 
} 

- (NSInteger)supportedInterfaceOrientations { 

return UIInterfaceOrientationMaskAllButUpsideDown; 

} 

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 
{ 
return (UIInterfaceOrientationMaskLandscape | UIInterfaceOrientationMaskPortrait); 
} 

의 ViewController가 Portait입니다 모드에서로드 만! .. pls는 저를 안내!

답변

0

// 그것은

EDIT: This is happening because Apple has changed the way of managing the Orientation of UIViewController. In iOS6 orientation handles differently. In iOS6 shouldAutorotateToInterfaceOrientation method is deprecated. View controllers (such as UINavigationController) do not consult their children to determine whether they should autorotate. By default, an app and a view controller’s supported interface orientations are set to UIInterfaceOrientationMaskAll for the iPad idiom and UIInterfaceOrientationMaskAllButUpsideDown for the iPhone idiom. 

If you want a specific view to be changed to the desired orientation you have to do some sort of subclass or category and override the autorotation methods to return the desired orientation. 

Place this code in your root view controller. This will help the UIViewController to determine its orientation. 

    //RotationIn_IOS6 is a Category for overriding the default orientation. 

    @implementation UINavigationController (RotationIn_IOS6) 

-(BOOL)shouldAutorotate 
    { 
     return [[self.viewControllers lastObject] shouldAutorotate]; 
    } 

    -(NSUInteger)supportedInterfaceOrientations 
    { 
    return [[self.viewControllers lastObject] supportedInterfaceOrientations]; 
    } 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
{ 
    return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation]; 
} 

@end 

Now you need to implement below methods (introduced in iOS6) in viewController for orientation 

- (BOOL)shouldAutorotate 
{ 
    //returns true if want to allow orientation change 
    return TRUE; 


} 
- (NSUInteger)supportedInterfaceOrientations 
{ 
    //decide number of origination tob supported by Viewcontroller. 
    return UIInterfaceOrientationMaskAll; 


} 

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation 
    { 
    //from here you Should try to Preferred orientation for ViewController 
    } 

And put your code inside the below method. Whenever device orientation is changed this method will be called: 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval)duration 
{ 
if([[[SampleApplicationAppDelegate instance].callInfoDictionary valueForKey:IS_CHAT] isEqualToString:NO_RESPONSE]) 
{ 
    int nAngle = 0; 
    BOOL bRet = NO; 

    switch (interfaceOrientation) { 
     case UIInterfaceOrientationPortrait: 
      nAngle = 90; 
      bRet = YES; 
      NSLog(@".......Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height); 

      _previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5); 

      NSLog(@"Preview = %f %f",_previewCamera.frame.size.width,_previewCamera.frame.size.height); 
      break; 

     case UIInterfaceOrientationPortraitUpsideDown: 
      nAngle = 270; 
      bRet = YES; 
      _previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2); 
      break; 

     case UIInterfaceOrientationLandscapeLeft: 
      nAngle = 0; 
      bRet = YES; 
      //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI*1.5); 
      break; 

     case UIInterfaceOrientationLandscapeRight: 
      nAngle = 180; 
      bRet = YES; 
      //_previewCamera.transform = CGAffineTransformMakeRotation(M_PI_2); 
      break; 

     default: 
      break; 
    }     
    }  

Edit: check your window, you need to add the controller on window as rootViewController rather than addSubview like below 

self.window.rootViewController=viewController; 
그것을 확인 작업