当每个新的视图控制器被推送时,调整导航控制器的大小以模态 UIModalPresentationFormSheet 呈现
Posted
技术标签:
【中文标题】当每个新的视图控制器被推送时,调整导航控制器的大小以模态 UIModalPresentationFormSheet 呈现【英文标题】:Resize Navigation Controller presented modally UIModalPresentationFormSheet as each new view controller is pushed 【发布时间】:2015-01-20 19:41:28 【问题描述】:设置:“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
【问题讨论】:
【参考方案1】:我花了一些时间来处理同样的问题。在 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,键盘呈现和关闭会自动将它们调整为错误的大小。不幸的是,我仍然没有解决这个问题。
【讨论】:
您可以通过在 viewWillAppear 中设置 navigationController?.preferredContentSize = CGSize(width: preferredContentSize.width, height: preferredContentSize.height) 来修复文本字段问题以上是关于当每个新的视图控制器被推送时,调整导航控制器的大小以模态 UIModalPresentationFormSheet 呈现的主要内容,如果未能解决你的问题,请参考以下文章