2012-08-24 25 views
5

Tôi đã tạo một ứng dụng thử nghiệm để tự làm quen với việc tạo bộ điều khiển chế độ xem bộ chứa tùy chỉnh. Nếu tôi xoay thiết bị khi ứng dụng lần đầu tiên bắt đầu hoặc sau khi chuyển sang một bộ điều khiển chế độ xem khác, chế độ xem mới sẽ thay đổi kích thước để chiếm toàn bộ màn hình như tôi dự định. Tuy nhiên, nếu tôi xoay sau khi ứng dụng bắt đầu, và sau đó chuyển sang bộ điều khiển chế độ xem mới, chế độ xem giữ kích thước dọc thay vì nhận ngắn hơn và rộng hơn (thực sự hơi khác một chút - từ 320,460 đến 300,480). Bộ điều khiển chế độ xem chính được cấp phát init'd trong ủy nhiệm ứng dụng (không có xib) và đặt làm bộ điều khiển chế độ xem gốc của cửa sổ. Đây là mã tôi có trong MasterViewController tôi (bộ điều khiển chứa tùy chỉnh):Lượt xem Không đổi kích thước sau khi xoay trong Bộ điều khiển vùng chứa tùy chỉnh

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    WelcomeController *welcome = [[WelcomeController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.currentController = welcome; 
    [self addChildViewController:welcome]; 
    [self.view addSubview:welcome.view]; 

    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft:)]; 
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft; 
    [self.view addGestureRecognizer:swipeLeft]; 
} 


- (void)swipeLeft:(UISwipeGestureRecognizer *)gesture { 
    if (gesture.state == UIGestureRecognizerStateRecognized) { 
     UIActionSheet *sheet =[[UIActionSheet alloc] initWithTitle:@"Select A Destination" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"welcome",@"Play",@"Scores", nil]; 
     [sheet showInView:self.view]; 
    } 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { 
    switch (buttonIndex) { 
     case 0:{ 
      if ([self.currentController class] != [WelcomeController class]) { 
       WelcomeController *welcome = [[WelcomeController alloc] initWithNibName:@"ViewController" bundle:nil]; 
       [self addChildViewController:welcome]; 
       [self moveToNewController:welcome]; 
      } 
      break; 
     } 
     case 1:{ 
      if ([self.currentController class] != [PlayViewController class]) { 
       PlayViewController *player = [[PlayViewController alloc] initWithNibName:@"PlayViewController" bundle:nil]; 
       [self addChildViewController:player]; 
       [self moveToNewController:player]; 
      } 
      break; 
     } 
     case 2:{ 
      if ([self.currentController class] != [HighScores class]) { 
       HighScores *scorer = [[HighScores alloc] initWithNibName:@"HighScores" bundle:nil]; 
       [self addChildViewController:scorer]; 
       [self moveToNewController:scorer]; 
      } 
      break; 
     } 
     case 3: 
      NSLog(@"Cancelled"); 
      break; 

     default: 
      break; 
    } 
} 

-(void)moveToNewController:(id) newController { 
    [self.currentController willMoveToParentViewController:nil]; 
    [self transitionFromViewController:self.currentController toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{} 
    completion:^(BOOL finished) { 
     [self.currentController removeFromParentViewController]; 
     [newController didMoveToParentViewController:self]; 
     self.currentController = newController; 
    }]; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES;//(interfaceOrientation == (UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft)); 
} 

Bất cứ ý tưởng tại sao điều này đang xảy ra (tôi không biết nếu điều này có nghĩa rằng quan điểm quan điểm điều khiển thạc sĩ không được thay đổi kích thước, nhưng khi tôi nhận được hành vi không thay đổi kích thước này, trình nhận dạng cử chỉ chỉ phản hồi trong chế độ xem hẹp, không phải trên toàn bộ màn hình)?

+3

Tôi phát hiện ra rằng nếu tôi sử dụng một tập tin xib và phân bổ initWithNibName: bundle: thay vì chỉ init, nó hoạt động chính xác. Vì vậy, nó dường như có một cái gì đó để làm với quan điểm mà bạn nhận được khi bạn chỉ cần làm init. Khi tôi đăng nhập xem, tuy nhiên, nó trông giống nhau cho dù từ đồng bằng init hoặc tập tin xib - cả hai đều có cùng một khung và cùng autoresize = W + H – rdelmar

+0

xác định autoresizingMask lập trình 'UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight' – yasirmturk

Trả lời

0

Bạn không gửi tin nhắn luân phiên cho con bạn xem bộ điều khiển. Ít nhất không phải trong mã bạn đăng. Sau khi chuyển đổi điều khiển con bạn thậm chí loại bỏ con trước đó từ mảng ChildViewControllers với [self.currentController removeFromParentViewController] vì vậy ngay cả khi bạn thực hiện - (BOOL) automaticForwardAppearanceAndRotationMethodsToChildViewControllers bạn luôn có duy nhất một ChildViewController trong ParentViewController của bạn.

Tôi đã làm việc này, vì vậy tôi sẽ dán bạn làm thế nào tôi đang làm điều này. Đầu tiên tôi tạo tất cả ViewControllers của tôi, thêm chúng như là bộ điều khiển xem con cho ParentViewController. Sau đó gọi phương thức didMoveToParentViewController:

//Controller1 
Controller1 *c1 = [[Controller1 alloc] init]; 
c1.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
[self addChildViewController:c1]; 
[c1 didMoveToParentViewController:self]; 

//Controller2 
Controller2 *c2 = [storyboard instantiateViewControllerWithIdentifier:@"c2"]; 
index.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
[self addChildViewController:c2]; 
[c2 didMoveToParentViewController:self]; 
c2.view.frame = m_contentView.frame; 
[self.view addSubview:c2.view]; //It is in initial screen so set it right away 
m_selectedViewController = c2; 

//Controller3 
Controller3 *c3 = [storyboard instantiateViewControllerWithIdentifier:@"c3"]; 
compare.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth; 
[self addChildViewController:c3]; 
[c3 didMoveToParentViewController:self]; 

m_controllers = [NSArray arrayWithObjects:c1, c2, c3, nil]; //Hmm now i think this is not needed, I can access viewController directly from self.childViewControllers array 

Sau đó, tôi thực hiện

- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers 
{ 
    return YES; 
} 

Chuyển sang xem con điều khiển được thực hiện với mã này

if (value < m_controllers.count) 
{ 
    UIViewController *contentViewController = [m_controllers objectAtIndex:value]; 

    contentViewController.view.frame = m_contentView.frame; 

    [self transitionFromViewController:m_selectedViewController toViewController:contentViewController duration:0 options:UIViewAnimationOptionTransitionNone animations:nil completion:^(BOOL finished) { 
     m_selectedViewController = contentViewController; 
     } 
    ]; 
} 

này nên được enought. Nhưng tôi đã có một số vấn đề với điều này vì vậy tôi gửi tin nhắn luân phiên cho trẻ em không hoạt động bản thân mình.

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    for (UIViewController *vc in m_controllers) 
    { 
     if(vc != m_selectedViewController) 
      [vc willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    } 
} 

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 
{ 
    for (UIViewController *vc in m_controllers) 
    { 
     if(vc != m_selectedViewController) 
      [vc willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration]; 
    } 
} 

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{ 
    for (UIViewController *vc in m_controllers) 
    { 
     if(vc != m_selectedViewController) 
      [vc didRotateFromInterfaceOrientation:fromInterfaceOrientation]; 
    } 
} 
+0

automaticForwardAppearanceAndRotationMethodsToChildViewController được khấu hao trong iOS 6, thay thế bằng shouldAutomaticallyForwardRotationMethods. Tuy nhiên, bạn không cần phải thực hiện nó vì mặc định là CÓ. Vì vậy, đó không phải là vấn đề. Tôi đã tìm thấy câu trả lời của tôi như tôi đã đề cập trong bình luận của tôi cho câu hỏi. – rdelmar

0

bằng tay thêm

bên

- (void)viewDidLoad 

và nó đã giải quyết được vấn đề,

vì một lý do giá trị bên trong kịch bản đã không được sử dụng tôi đoán

Các vấn đề liên quan