2015-08-08 37 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]; 
}