문제

UITabBarController가 있고 각 탭은 필요에 따라 스택 새 컨트롤러를 푸시하는 다른 UIViewController를 처리합니다.이 탭 중 두 개에는 특정 컨트롤러에 도달했을 때 iPhone을 회전하고 가로 모드로 보기를 시각화하는 기능이 필요합니다.많은 어려움을 겪은 후에 shouldAutorotateToInterfaceOrientation을 재정의하려면 UITabBarController를 서브클래싱하는 것이 필수라는 것을 알게 되었습니다.그러나 구현에서 단순히 YES를 반환하면 다음과 같은 바람직하지 않은 부작용이 발생합니다.

iPhone을 회전하면 모든 탭의 모든 컨트롤러가 자동으로 가로 모드로 전환됩니다.

NO를 반환하기 위해 각 컨트롤러에서 shouldAutorotateToInterfaceOrientation을 재정의해도 작동하지 않습니다.iPhone을 회전하면 컨트롤러가 가로 모드로 전환됩니다.

서브클래싱된 UITabBarController에서 다음과 같이 shouldAutorotateToInterfaceOrientation을 구현했습니다.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if([self selectedIndex] == 0 || [self selectedIndex] == 3)
        return YES;

    return NO;
}

그래서 내가 관심 있는 두 탭만 실제로 가로 모드를 지원합니다.특정 탭의 스택에 있는 특정 컨트롤러에 대해 가로 모드를 지원하는 방법이 있습니까?

나는 성공하지 못한 채 다음과 같은 것을 시도했습니다.

(BOOL)인터페이스 방향으로 자동 회전해야 합니다:(UI인터페이스 방향)인터페이스 방향 {

if([self selectedIndex] == 0 || [self selectedIndex] == 3)
{   
   if ([[self selectedViewController] isKindOfClass: [landscapeModeViewController class]])
           return YES;
    }

     return NO;

}

또한 대리자 메서드 didSelectViewController를 사용해 보았지만 성공하지 못했습니다.어떤 도움이라도 대단히 감사하겠습니다.감사합니다.

도움이 되었습니까?

해결책

이것은 나를 위해 효과가있었습니다.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    if(self.selectedIndex == 0 && [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[MyViewController class]])
        return YES;
    else
        return NO;
}

다른 팁

다음은 UitabbarController 로의 연장이 있습니다. shouldAutorotateToInterfaceOrientation 현재 선택된 어린이 컨트롤러에. 이 확장자를 사용하면 더 이상 Uitabbarcontroller를 서브 클래스 할 필요가 없으며 사용할 수 있습니다. shouldAutorotateToInterfaceOrientation 당신의 컨트롤러에서는 기대할 수 있습니다.

Uitabbarcontroller+autorotate.h :

#import <UIKit/UIKit.h>

@interface UITabBarController (Autorotate)
@end

Uitabbarcontroller+autorotate.m :

#import "UITabBarController+Autorotate.h"

@implementation UITabBarController (Autorotate)

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    UIViewController *controller = self.selectedViewController;
    if ([controller isKindOfClass:[UINavigationController class]])
        controller = [(UINavigationController *)controller visibleViewController];
    return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end

문제없이 (내 앱의 탭 바 컨트롤러에서) 잠시 동안 이것을 사용할 수있었습니다.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
     return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

그렇게하면 적절한 VC에서 우리는 진짜 이 경우 사진 갤러리 뷰를 확인하십시오 (다른 무엇입니까?) :

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

내 갤러리보기는 주어진 NAV 컨트롤러의 스택 상단에도 없습니다. 여전히 호출됩니다.

아아, 나는 이것을 발견했다 그렇지 않습니다 VC가 숨어있을 때 너무 잘 작동합니다. MoreViewController (네 가지 기본 탭과 반대로). 이 경우 내 갤러리 VC는 결코 호출되지 않습니다. 내가 모두 호출 한 VC가 실제로 선택한 탭의 NAV 컨트롤러이기 때문이라고 생각합니다. 그 다음에 사물을 적절한 VC로 전파합니다.이 경우 내 사진 갤러리 vc. 그러나 더 많은 VC의 경우, 상황이 그렇게 멋지게 작동하지 않습니다 ... Aaaand는 거기에서 내리막 길로 돌아갑니다. :

Andreas (이 스레드의 다른 곳 참조)의 수정을 사용하려고 시도했습니다. 단서를 환영합니다!

나는 Uitabbarcontroller와 함께 일할 때와 같은 문제를 해결했습니다. 나는 어떤 uiviewControllers가 회전 할 수 있었는지 제어해야했고 어떤 것이 아닌지를 제어해야했다. 내 주요 문제는 더 많은 탭이었습니다. 더 많은 탭에 포함 된 UIViewControllers가 회전하는 것을 원하지 않았습니다.

내 해결책은 MyTabbarController라고 불렀던 나만의 UitabbarController를 만드는 것이 었습니다.

@interface MyTabBarController : UITabBarController <UITabBarDelegate> {

}

그런 다음 whilsautorotateTointerfaceorientation 방법을 구현했습니다.

@implementation MyTabBarController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 UIViewController *controller = [self selectedViewController];

 if ((controller == [self moreNavigationController]) || ([self selectedIndex] == 4))
 {
  return interfaceOrientation == UIInterfaceOrientationPortrait;
 }

 return [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

@end

더 많은 탭이 선택되었는지 발견해야했습니다. 이것은 두 단계 프로세스입니다. 더 많은 탭이 처음에 선택되면 처음에 API는 4보다 높은 선택된 index를 반환하므로 선택한 컨트롤러를 MorenavigationController와 비교해야했습니다.

더 많은 탭 내에서 UIViewController가 선택되면 선택한 index는 최종적으로 4이지만 선택한 콘트롤러는 더 이상 MorenavigationController가 아니라 UIViewController가 선택된 것입니다.

그만큼 if ((컨트롤러 == [self morenavigationcontroller]) || ([self selectedIndex] == 4)) 이 문제를 처리합니다.

이제 응용 프로그램을 실행하면 More 탭에서 UIViewControllers가 회전되지 않습니다. 이것이 내가 한 것과 같은 문제에 빠지는 다른 개발자들이 도움이되기를 바랍니다.

에밀리오

내가 여기와 다른 곳에서 본 것에서 나는 방법을 사용하는 솔루션을 함께 꿰매었다. shouldAutorotate 오래된 이후 shouldAutorotateToInterfaceOrientation 더 이상 사용되지 않았습니다.

나는 그것을 UitabbarController에 카테고리 안에 배치했습니다. 나는 이것이 허용되기를 바랍니다!

// call to method shouldAutorotate replaces call to method shouldAutorotateToInterfaceOrientation (deprecated)
-(BOOL)shouldAutorotate
{ // check whether selected view controller should autorotate    
  UIViewController *controller = self.selectedViewController;
  if ([controller isKindOfClass:[UINavigationController class]])
    { // in case it is a navigation controller: get visible view of that
      controller = [(UINavigationController *)controller visibleViewController];
    }
  return [controller shouldAutorotate];
}

고마워요, 고마워요, 고마워요.이 작업을 수행하는 방법을 알아내는 데 2일이 걸렸습니다.NavigationController가 포함된 tabBarController가 있을 때 도움이 되는 모든 정보는 다음과 같습니다.

-(BOOL)InterfaceOrientation으로 자동 회전해야 함:(UIInterfaceOrientation)interfaceOrientation {

UIViewController *controller = self.selectedViewController;
if ([controller isKindOfClass:[UINavigationController class]])
    controller = [(UINavigationController *)controller visibleViewController];

if([controller isKindOfClass:[LOCviewcontroller class]])
    return YES;
else
    if([controller isKindOfClass:[personWebSiteView class]])
        return YES;
else return NO; 

}

신참 코더의 코드에 대한 비평은 언제나 감사합니다...잭

Uitabbarcontroller를 서브 클래스해도 괜찮습니까 (위의 허용 답변에서 제안 된 바와 같이)?

나는 사과가 "Uitabbarcontroller 또는 uinavigationcontroller를 서브 클래스하지 않아야한다"와 같은 것을 이해했거나 오해 했습니까?

그래도; 나는 찾았다 이 튜토리얼 그들은 UITABBARCONTROLLER를 배치하는 UIViewController를 서브 클래스합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top