2015-08-08 38 views
0

我试图显示一个UIAlertController与一些操作按钮。它在iPhone上运行良好,因为它从设备底部弹出。我有一个ipad的问题,UIAlertController没有正确居中。它显示了一点偏右。我可以通过减去150f来从x坐标中减去。有没有办法让它居中?UIAlertController - iOS 8 - iPad - 没有正确居中

UIAlertController * view= [UIAlertController 
          alertControllerWithTitle:@"My Title" 
          message:@"Select your Choice" 
          preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* ok = [UIAlertAction 
        actionWithTitle:@"OK" 
        style:UIAlertActionStyleDefault 
        handler:^(UIAlertAction * action) 
        { 
         //Do some thing here 
         [view dismissViewControllerAnimated:YES completion:nil]; 

        }]; 
UIAlertAction* cancel = [UIAlertAction 
         actionWithTitle:@"Cancel" 
         style:UIAlertActionStyleDefault 
         handler:^(UIAlertAction * action) 
         { 
          [view dismissViewControllerAnimated:YES completion:nil]; 

         }]; 


[view addAction:ok]; 
[view addAction:cancel]; 



view.popoverPresentationController.sourceView = self.view; 
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0, 0.0, 0.0); 
[self presentViewController: view animated:YES completion:nil]; 
+1

什么是'self.view.frame'相对于'[UIScreen mainScreen] .bounds'?它是否完美居中? – Stuart

回答

1

如果你有简单的动作,然后更好地使用Alertstyle,它会自动居中或者如果你坚持使用Actionsheet风格,尝试设置popoverPresentationController.permittedArrowDirections = 0这个作品有固定的方向很好,但如果你在iPad上旋转失败。

// Alert 
- (void) showAlert 
{ 
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"TEst" message:@"test Message" preferredStyle:UIAlertControllerStyleAlert]; 
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { 

    }]; 

    [alertController addAction:cancelAction]; 


    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil]; 
}