我花了一些时间处理同样的问题。在 iOS8 的模态导航控制器上推送视图控制器导致视图控制器与根视图控制器具有相同的大小,而对于 iOS7,第二个视图控制器具有模态表单的默认大小(540.f、620.f) . 到目前为止,对于 iOS7 和 iOS8,我能想到的唯一可行的解决方案如下:
ViewController1.m
@interface ViewController1 ()
@end
@implementation ViewController1
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.view.superview.bounds = CGRectMake(0, 0, VC1_WIDTH, VC1_HEIGHT);
}
- (void)nextTapped:(id)sender {
ViewController2 *vc2 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController2"];
[self.navigationController pushViewController:vc2 animated:NO];
// call after push
self.navigationController.view.superview.bounds = CGRectMake(0, 0,VC2_WIDTH,VC2_HEIGHT);
}
@end
呈现 ViewController1:
ViewController1 *vc1 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:vc1];
navVC.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navVC animated:NO completion:nil];
视图控制器2.m
#import "ViewController2.h”
@interface ViewController2 ()
@end
@implementation ViewController2
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.view.superview.bounds = CGRectMake(0, 0, VC2_WIDTH, VC2_HEIGHT);
}
- (void)nextTapped:(id)sender {
ViewController3 *vc3 = [storyboard instantiateViewControllerWithIdentifier:@"ViewController3”];
[self.navigationController pushViewController:vc3 animated:NO];
self.navigationController.view.superview.bounds = CGRectMake(0, 0,VC3_WIDTH,VC3_HEIGHT);
}
- (void)backTapped:(id)sender {
self.navigationController.view.superview.bounds = CGRectMake(0, 0,VC1_WIDTH,VC1_HEIGHT);
[self.navigationController popViewControllerAnimated:NO];
}
@end
ViewController3.m
#import "ViewController3.h”
@interface ViewController3 ()
@end
@implementation ViewController3
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.navigationController.view.superview.bounds = CGRectMake(0, 0, VC3_WIDTH, VC3_HEIGHT);
}
- (void)backTapped:(id)sender {
self.navigationController.view.superview.bounds = CGRectMake(0, 0,VC2_WIDTH,VC2_HEIGHT);
[self.navigationController popViewControllerAnimated:NO];
}
@end
请注意,如果您向模态导航控制器上推送的视图控制器添加文本输入字段,对于 iOS8,键盘呈现和关闭将自动将它们调整为错误的大小。不幸的是,我仍然没有解决这个问题的正确方法。