2

设置: “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
4

1 回答 1

4

我花了一些时间处理同样的问题。在 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,键盘呈现和关闭将自动将它们调整为错误的大小。不幸的是,我仍然没有解决这个问题的正确方法。

于 2015-01-27T12:16:22.440 回答