2016-04-22 126 views
1

有沒有辦法獲得外部屏幕邊框和酒吧按鈕項目之間的距離。我正在考慮類似如何獲得狀態欄高度?屏幕邊框和酒吧按鈕項目之間的距離

(我問這個,因爲這取決於設備是不同的)

enter image description here 這是一些試驗代碼:

let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, view.frame.size.width, 64)) 
    let navigationBar.backgroundColor = UIColor.redColor() 

    let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil) 
    navigationItem.leftBarButtonItem = barButtonItem 
    navigationBar.items = [navigationItem] 

    let buttonItemView = barButtonItem.valueForKey("view") as! UIView 
    let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: UIApplication.sharedApplication().keyWindow?.rootViewController?.view) 
    let xCoordinateMin = CGRectGetMinX(frame) 
    let yCoordinateMax = CGRectGetMaxY(frame) 
    let yCoordinateMin = CGRectGetMinY(frame) 

    let label = UILabel(frame: CGRectMake(CGFloat(xCoordinateMin), CGFloat(yCoordinateMin), 100, yCoordinateMax - yCoordinateMin)) 
    label.backgroundColor = UIColor.greenColor() 

但它仍然給我0作爲XMIN,雖然它似乎與工作y座標..

enter image description here

回答

0

解決與此代碼:

let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, view.frame.size.width, 124)) 
    navigationBar.backgroundColor = UIColor.redColor() 

    let barButtonItem = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: nil) 
    navigationItem.leftBarButtonItem = barButtonItem 
    navigationBar.items = [navigationItem] 

    let buttonItemView = barButtonItem.valueForKey("view") as! UIView 
    let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: nil) 
    let xCoordinateMin: CGFloat = CGRectGetMinX(frame) 
    let xCoordinateMax: CGFloat = CGRectGetMaxX(frame) 
    let yCoordinateMax: CGFloat = CGRectGetMaxY(frame) 
    let yCoordinateMin = CGRectGetMinY(frame) 

    let label = UILabel(frame: CGRectMake(abs(xCoordinateMin), yCoordinateMin, xCoordinateMax - xCoordinateMin, yCoordinateMax - yCoordinateMin)) 
    label.backgroundColor = UIColor.greenColor() 

訣竅是宣佈let xCoordinateMinCGFloat,因爲那時它被認爲是負值。然後在創建標籤時只需使用abs()即可獲取絕對值。您現在有一個與您的酒吧按​​鈕項目大小完全相同的標籤!

enter image description here

-1
UIApplication.sharedApplication().statusBarFrame.height 
+0

好的,謝謝,我已經走這..但我想知道的外屏幕邊框和酒吧按鈕項目之間的距離。 – manu

2

通過外屏,我假設你正在談論主窗口。

您可以嘗試欄按鈕的幀轉換成窗口的座標,以便找到到每個方向上的距離:

let buttonItemView = barButtonItem.valueForKey("view") as! UIView 
let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: nil) 

print("Distance to left: ", CGRectGetMinX(frame)) 
print("Distance to top: ", CGRectGetMinY(frame)) 

然而,主窗口接收旋轉事件,並將它們傳遞到,這意味着它不控制器更改其框架大小,以便上述解決方案無法在橫向模式下正常工作。

你可以試試按鈕的矩形轉換爲根視圖控制器的座標系統,但現在你必須要在狀態欄的高度考慮:

let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: UIApplication.sharedApplication().keyWindow?.rootViewController?.view) 
+0

與CGRectGetMinX(框架)我得到0 ..但我不應該得到的距離,而是主窗口? – manu

+0

那麼你應該將''toView'參數的'nil'傳遞給'convertRect'方法。例如:'buttonItemView.superview!.convertRect(buttonItemView.frame,toView:nil)' – ozgur

+0

似乎沒有工作.. – manu