1

设置:“VC1”有根视图控制器“VC2”创建“NavigationVC”,并与演讲风格UIModalPresentationFormSheet模态呈现它。 “VC2”显示在导航控制器内部正确大小的屏幕中间。调整大小导航控制器呈现模态UIModalPresentationFormSheet为每一个新的视图控制器推

期:随着我继续将视图控制器推到模态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

我花了一些时间处理同样的问题。在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,键盘演示和解除会自动将其大小调整为错误的大小。不幸的是,我仍然没有妥善解决这个问题。

+0

您可以通过在viewWillAppear中设置navigationController?.preferredContentSize = CGSize(width:preferredContentSize.width,height:preferredContentSize.height)来解决文本字段问题 – 2017-04-13 02:39:13

相关问题