2013-06-06 2 views
5

UIWebView (아래 코드 참조) 내에 YouTube 동영상을 재생하려고합니다.iOS 6의 YouTube 플레이어 오리엔테이션 문제

모든 것이 정상적으로 작동하지만 YouTube 플레이어는 iOS 6에서 방향 변경을 지원하지 않습니다. 전체 앱이 세로 모드에만 있습니다.

어떻게이 문제를 해결할 수 있습니까? 어떤 도움을 주셔서 감사합니다.

내가 사용하고있는 코드는 다음과 같습니다 : 당신이해야 할 것입니다 무엇

- (void)viewDidLoad 

{   
    [super viewDidLoad]; 
    float width = 300.0f; 
    float height = 300.0f; 
    youTubeURL = @"http://www.youtube.com/embed/ZiIcqZoQQwg"; 
    UIWebView *wv = [[UIWebView alloc] init]; 
    wv.frame = CGRectMake(10, 80, width, height); 

    NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ; 
    [html appendString:@"<html><head>"]; 
    [html appendString:@"<style type=\"text/css\">"]; 
    [html appendString:@"body {"]; 
    [html appendString:@"background-color: black;"]; 
    [html appendString:@"color: white;"]; 
    [html appendString:@"}"]; 
    [html appendString:@"</style>"]; 
    [html appendString:@"</head><body style=\"margin:0\">"]; 
    [html appendFormat:@"<embed id=\"yt\" src=\"%@\"", youTubeURL]; 
    [html appendFormat:@"width=\"%0.0f\" height=\"%0.0f\"></embed>", 300.0f, 300.0f]; 
    [html appendString:@"</body></html>"]; 
    [wv loadHTMLString:html baseURL:nil]; 
    [self.view addSubview:wv]; 
} 
+0

플레이어의보기 컨트롤러 (전체 스크린 비디오보기 컨트롤러)가 회전하려고합니까? 또는 플레이어가 전체 화면으로 표시 할 수 없으며 'UIWebview'(또는 뷰 컨트롤러가 포함 된)를 회전 시키길 원합니까? –

+0

플레이어의보기 컨트롤러를 회전하여 장치 방향에 따라 회전하려고합니다. –

답변

2

가로 및 세로 지원이 모두 필요한 경우 "대상"-> 요약 -> "지원되는 인터페이스 방향"에서 응용 프로그램의 지원 모드를 설정해야합니다.

플레이어를로드하기위한 코드가 정확합니다. 오리엔테이션 지원 만 추가하면됩니다.

자동 레이아웃을 사용하는 경우 xib의 제약 조건을 확인하십시오. 그렇지 않은 경우 NiravPatel에서 설명한대로 각 방향 변경시 웹보기 용 프레임을 설정합니다. 당신이 회전하는 하나의 뷰 컨트롤러를 원한다면

, 모든 뷰 컨트롤러에이 추가

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr‌​ientation { return NO; } 

당신은 회전 및 기타 viewControllers에 NO 반환해야의 ViewController에 YES를 반환 할 수 있습니다.

+0

이렇게하면 전체 앱이 회전합니다. 전체보기가 아닌 비디오를 재생하는보기 컨트롤러 만 필요합니다. –

+0

전체 앱에 인터페이스 방향 지원을 추가해야합니다. 그런 다음 다른보기에서 autoRotation을 제한 할 수 있습니다. 아래 코드를 확인하십시오. 다른 뷰 컨트롤러에서 NO를 반환하고 필요한 한 뷰에서 YES를 반환 할 수 있습니다. - (BOOL) shouldAutorotateToInterfaceOrientation : (UIInterfaceOrientation) interfaceOrientation { return NO; } – BrianChristo

3

아래처럼 ... 당신이 당신의 장치를 회전 할 때

먼저 호출되는있는 viewDidLoad 한 통지를 할 ..

-(void)ViewDidLoad 
{ 
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(rotate:) 
              name:UIDeviceOrientationDidChangeNotification 
              object:nil]; 
} 

이제 통지 방법라는 이름의 "회전"호출 그래서 우리는 다음과 같은 메소드를 구현해야합니다.

#pragma mark - Rotate Screen Method 
- (void)rotate:(NSNotification *)n { 

switch ([[UIDevice currentDevice] orientation]) { 
    case UIDeviceOrientationLandscapeLeft: 
     yourwebview.transform = CGAffineTransformMakeRotation(M_PI/2); 
     yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame 
     break; 
    case UIDeviceOrientationLandscapeRight: 
     yourwebview.transform = CGAffineTransformMakeRotation(-M_PI/2); 
     yourwebview.frame = CGRectMake(0, 0,768, 1024);//you can set any frame 
     break; 
    case UIDeviceOrientationPortrait: 
     yourwebview.transform = CGAffineTransformIdentity; 
     yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame 
     break; 
    case UIDeviceOrientationPortraitUpsideDown: 
     yourwebview.transform = CGAffineTransformMakeRotation(M_PI); 
     yourwebview.frame = CGRectMake(0, 0, 768, 1024);//you can set any frame 
     break; 
    default: 
     break; 
    } 
} 

그게 전부입니다.

+0

사실 나는 (YouTube 프레임에서 재생 버튼을 누른 후) 비디오를 재생하는보기를 회전하여 장치 방향에 따라 회전하려고합니다. –

+0

@AnoopK 해결 방법을 찾을 수 있었습니까? –