解决presentViewController UIModalPresentationPageSheet自定义尺寸在ios中不居中的问题
通常用来做pad app 登录框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
方法一 最常用方法 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation ==UIInterfaceOrientationLandscapeRight); } LoginViewController *login = [[LoginViewController alloc]init]; login.modalPresentationStyle = UIModalPresentationPageSheet; [self presentViewController:login animated:YES completion:^(void){ }]; login.view.superview.bounds = CGRectMake(0, 0, 450, 200); 这种方法是最普遍的方法 再ios5 和 6 中显示正常 但是再ios7 中弹出的formsheet 不能居中显示 第二种方法 login.modalPresentationStyle = UIModalTransitionStyleCrossDissolve; 把被弹出vc 弹出模式改变成UIModalTransitionStyleCrossDissolve 会在ios7中居中 第三种方法: 这种方法会在弹出之前调整view的大小 有时候 会闪烁 LoginViewController *login = [[LoginViewController alloc]init]; [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:login]; login.modalPresentationStyle = UIModalPresentationPageSheet; [self presentViewController:login animated:YES completion:^{ }]; - (void) hackModalSheetSize:(CGSize) aSize ofVC:(UIViewController *) aController; { void (^formSheetBlock) (void) = ^{ int preferredWidth = aSize.width; int preferredHeight = aSize.height; CGRect frame = CGRectMake((int) 1024/2 - preferredWidth/2, (int) 768/2 - preferredHeight/2, preferredWidth, preferredHeight); aController.view.superview.frame = frame; if([aController respondsToSelector:@selector(edgesForExtendedLayout)]) { //ios7 aController.view.superview.backgroundColor = [UIColor clearColor]; } else { // < ios7 UIImageView *backgroundView = [aController.view.superview.subviews objectAtIndex:0]; [backgroundView removeFromSuperview]; } }; //on ios < 7 the animation would be not as smooth as on the older versions so do it immediately if(![self respondsToSelector:@selector(edgesForExtendedLayout)]) { formSheetBlock(); return; } double delayInSeconds = .05; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ formSheetBlock(); }); } 第四种方法 推荐 在想要弹出的ViewController 添加改变大小代码 - (void)viewWillLayoutSubviews{ [super viewWillLayoutSubviews]; self.view.superview.bounds = CGRectMake(0, 0, 450, 200); } 弹出方法: LoginViewController *login = [[LoginViewController alloc]init]; login.modalPresentationStyle = UIModalPresentationPageSheet; [self presentViewController:detail animated:YES completion:^(void){ }]; |