2016-04-22 16 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() 

しかし、それはまだそれが、私にXMINとして0を与えます

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 
.. y座標で動作するようです10
+0

ええ、私はすでにこれを行って感謝..しかし、私は外側スクリーンの境界線とバーボタンアイテム間の距離を知りたいです。 CGRectGetMinX(フレーム)の – 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

を取得します。代わりにメインウィンドウとの距離を取得しないでください。 – manu

+0

次に、 'toView'パラメータの' nil'を 'convertRect'メソッドに渡すべきです。例: 'buttonItemView.superview!.convertRect(buttonItemView.frame、toView:nil)' – ozgur

+0

は動作していないようです。 – manu

関連する問題