在推送每个新视图控制器时,调整大小导航控制器的模式UIModalPresentationFormSheet

时间:2015-01-20 19:41:28

标签: ios objective-c autolayout modalviewcontroller uimodalpresentationstyle

设置:' VC1'创建' NavigationVC'使用根视图控制器' VC2'并使用演示文稿样式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

1 个答案:

答案 0 :(得分: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];

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,键盘演示和解除将自动将其调整为错误的大小。不幸的是,我仍然没有妥善解决这个问题。