最新消息:iOS编程开发交流群(6906921) ,Mac.Cocoa开发交流群(7758675) 欢迎iOS/macOS开发编程爱好及学习者加入!

多种方法解决presentViewController自定义尺寸在ios7中不居中的问题

iOS 天狐 14460浏览 0评论

解决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){

                    }];

 

转载请注明:天狐博客 » 多种方法解决presentViewController自定义尺寸在ios7中不居中的问题

微信 OR 支付宝 扫描二维码
为天狐 打赏
非常感谢你的支持,哥会继续努力!
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址