解决presentViewController UIModalPresentationPageSheet自定义尺寸在ios中不居中的问题
通常用来做pad app 登录框
方法一 最常用方法
- (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){
}];