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)?
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
xác định autoresizingMask lập trình 'UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight' – yasirmturk