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

相關問題