2011-08-02 5 views
3

가로 전용 응용 프로그램에 대한 내보기를 처리하기 위해 UIViewController 하위 클래스가 있습니다. 가로보기를 자동으로 반영하기 위해 크기를 조정하고 싶지만보기에는 어렵습니다. 그러나 하위보기가 있습니다.첫 번째 UIViewController의 뷰를 자동 크기 조정으로 설정하는 방법은 무엇입니까?

다음은 재생 코드입니다.

@interface MyViewController : UIViewController { 
    UIView *subview; 
} 

@implementation MyViewController 

- (id)init 
{ 
    self = [super init]; 
    if (self) { 
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

    subview = [[UIView alloc] initWithFrame:self.view.frame]; 
    subview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 
    [self.view addSubview:subview]; 
    } 

    return self; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { 
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation); 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { 
    NSLog(@"My view's frame (%f,%f)", self.view.frame.size.width, self.view.frame.size.height); 
    NSLog(@"Subview's frame (%f,%f)", subview.frame.size.width, subview.frame.size.height); 
} 

@end 

그리고 ...

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; 
    self.window.rootViewController = [[MyViewController alloc] init]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

슈퍼 간단합니다. 그리고 부팅 할 때 다음 메시지가 기록됩니다.

My view's frame (320.000000,480.000000) 
Subview's frame (480.000000,320.000000) 

하위보기의 크기는 올바르게 조정되지만 부모보기에는 여전히 세로 방향의 프레임이 있습니다.

가로 모드에 맞게 최상위보기 크기를 조정하려면 어떻게해야합니까?

답변

0

이 시도 :

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 

    if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 

     //is portrait 

     view.frame = CGRectMake(0,0,320,480) 


    } else { 

     //is landscape 

     view.frame = CGRectMake(0,0,480,320); 

    } 
} 

희망이 도움이! 당신의 ViewController

self.view.frame = [[UIScreen mainScreen] bounds]; 
+0

불행히도이 코드는 단지 하드 코드로되어 있습니다 만, 나는 피하고 싶습니다. 현재 장치 생태계에서도 코드는 장치 관용구, 전화 또는 패드를 확인하기 위해 다른 지점이 필요합니다. –

2

사용이 당신은 윈도우 자동 크기 파단을 허용해야한다 :

window.autoresizesSubviews=YES; 

희망이 도움이!

+0

이것을 사용하면 scrollview가 작동하지 않습니다. ?!?! – KennyVB

2

에서

+0

감사합니다. 자동 레이아웃을 사용하지 않을 때 속성 관리자 아래의 첫 번째보기에서 자동보기 하위보기를 선택하십시오. –

1
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

viewdidload에 추가하십시오. 스토리 보드에서 ViewControllers보기 속성이보기에 할당되었음을 확인하십시오.

관련 문제