我需要顯示一些彈出窗口,並且我需要右側導航欄按鈕的位置。目前我使用的如何找到UINavigationBar的中心座標項
x = self.view.frame.width - 30
y = self.view.frame.origin.y + 60
但我認爲這不是一個好辦法,因爲iPad的屏幕在橫向查看view.frame.origin.y有點DIFF到iPhone在同樣的情況。
我需要顯示一些彈出窗口,並且我需要右側導航欄按鈕的位置。目前我使用的如何找到UINavigationBar的中心座標項
x = self.view.frame.width - 30
y = self.view.frame.origin.y + 60
但我認爲這不是一個好辦法,因爲iPad的屏幕在橫向查看view.frame.origin.y有點DIFF到iPhone在同樣的情況。
你可以得到它這樣
if let rightBarButton = self.navigationItem.rightBarButtonItem {
if rightBarButton.valueForKey("view") != nil {
// here you can access the frame like this buttonView.frame
}
}
也許這可以給你正確的位置轉換成超認爲,從拉雅答案更新:
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)
}
}
使用此代碼self.yourView.center =自.view.center; – Sommm