在推送每个新视图控制器时调整导航控制器的大小以模态方式呈现 UIModalPresentationFormSheet

Resize Navigation Controller presented modally UIModalPresentationFormSheet as each new view controller is pushed

设置: 'VC1' 使用根视图控制器 'VC2' 创建 'NavigationVC' 并使用呈现样式 UIModalPresentationFormSheet 以模态方式呈现它。 'VC2' 以正确的尺寸显示在屏幕中间的导航控制器内。

问题: 当我继续将视图控制器推到模式 NavVC 上时,我希望它们调整大小。推送的每个视图控制器中我的 preferredContentSize 的 NSLog 验证我的约束是否正确并且大小实际上不同。但是,我已经进行了广泛的实验,但还没有弄清楚如何在呈现模态后更改模态的大小。

@implementation VC1()

- (void) viewDidLoad{
    VC1* vc1 = [self getNextVC];
    NavVC* navVC = [[UINavigationViewController alloc] initWithRootViewController:vc1];
    [navVC setModalPresentationStyle:UIModalPresentationFormSheet];
    [self presentViewController:navVC animated:YES completion:nil];
}

@end

@implementation NavVC()

- (CGSize) preferredContentSize{
    CGSize size = [[self topViewController] preferredContentSize];
    return size;
}

@end


@implementation VC2()

- (CGSize) preferredContentSize{
    CGSize size = [[self view] systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size;
}

@end

我花了一些时间来处理同样的问题。在 iOS8 的模态导航控制器上推动视图控制器导致视图控制器与根视图控制器具有相同的大小,而对于 iOS7,第二个视图控制器具有模态形式的默认大小 sheet(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];

ViewController2.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,键盘呈现和关闭将自动将它们调整为错误的大小。不幸的是,我仍然没有正确解决这个问题。