2016-11-23 23 views
0

我需要顯示一些彈出窗口,並且我需要右側導航欄按鈕的位置。目前我使用的如何找到UINavigationBar的中心座標項

x = self.view.frame.width - 30 
y = self.view.frame.origin.y + 60 

但我認爲這不是一個好辦法,因爲iPad的屏幕在橫向查看view.frame.origin.y有點DIFF到iPhone在同樣的情況。

+0

使用此代碼self.yourView.center =自.view.center; – Sommm

回答

0

你可以得到它這樣

if let rightBarButton = self.navigationItem.rightBarButtonItem { 
    if rightBarButton.valueForKey("view") != nil { 
      // here you can access the frame like this buttonView.frame 
    } 
} 
0

也許這可以給你正確的位置轉換成超認爲,從拉雅答案更新:

if let rightBarButton = self.navigationItem.rightBarButtonItem { 
     if let buttonView = rightBarButton.valueForKey("view") { 
      let frame = self.navigationController?.view.convertRect(buttonView.frame, toView: self.view) 
      print("frame \(frame)") //(330.5, 6.0, 40.0, 30.0) 
     } 
} 
+0

'UIBarButtonItem'has not member'view' – Vahid

+0

這是swift 2,也許它在swift 3中被刪除,你可以嘗試結合來自Rajat的答案來獲取框架並在此使用 – Tj3n

+0

我正在swift 2上工作;) – Vahid