【问题标题】:UIAlertController - iOS 8 - iPad - Not Centering ProperlyUIAlertController - iOS 8 - iPad - 未正确居中
【发布时间】:2015-10-06 00:11:13
【问题描述】:

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

   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];

【问题讨论】:

  • 相对于[UIScreen mainScreen].boundsself.view.frame 是什么?是否完全居中?

标签: ios ipad uialertcontroller


【解决方案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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多