2

Setup: 'VC1' creates 'NavigationVC' with a root view controller 'VC2' and presents it modally with presentation style UIModalPresentationFormSheet. 'VC2' shows up inside the nav controller in the middle of the screen with the correct size.

Issue: As I continue to push view controllers onto the modal NavVC I would like them to resize. NSLog of my preferredContentSize in each view controller that is pushed verifies that my constraints are correct and the sizes are in fact different. However I have experimented extensively and have not figured out how to change the size of the modal after it has been presented.

@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
Garfbargle
  • 3,222
  • 2
  • 17
  • 17

1 Answers1

4

I’ve spent some time dealing with the same problem. Pushing the view controller on modal navigation controller for iOS8 resulted in having the view controller with the same size as the root view controller, while for iOS7 this second view controller had the default size of modal form sheet (540.f, 620.f). The only working solution, I could come up with so far, for both iOS7 and iOS8 is as follows:

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

Presenting 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

Please note, if you’re adding text input fields to those view controllers pushed on modal navigation controller, for iOS8, keyboard presentation and dismissal will automatically resize them to the wrong size. Unfortunately I still don’t have proper fix for this issue.

Marija P.
  • 166
  • 3
  • You can fix the text field issue by setting navigationController?.preferredContentSize = CGSize(width: preferredContentSize.width, height: preferredContentSize.height) in viewWillAppear – Ben Williams Apr 13 '17 at 02:39