Ios 屏幕边框和条形按钮项之间的距离
是否有一种方法可以获取屏幕外边框和条形按钮项之间的距离。我在想类似的事情,比如如何获得状态栏的高度 (我问这个问题,因为它因设备而异。) 这是一些试用代码:Ios 屏幕边框和条形按钮项之间的距离,ios,swift,uibarbuttonitem,Ios,Swift,Uibarbuttonitem,是否有一种方法可以获取屏幕外边框和条形按钮项之间的距离。我在想类似的事情,比如如何获得状态栏的高度 (我问这个问题,因为它因设备而异。) 这是一些试用代码: let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, view.frame.size.width, 64)) let navigationBar.backgroundColor = UIColor.redColor() let barButtonI
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坐标一起工作
通过外部屏幕,我假设您正在谈论主窗口 可以尝试将条形按钮的框架转换为窗口的坐标,以查找到每个方向的距离:
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))
但是,主窗口接收旋转事件并将其传递给控制器,这意味着它不会更改其帧大小,因此上述解决方案在横向模式下无法正常工作
可以尝试将按钮的rect转换为根视图控制器的坐标系,但现在必须考虑状态栏的高度:
let frame = buttonItemView.superview!.convertRect(buttonItemView.frame, toView: UIApplication.sharedApplication().keyWindow?.rootViewController?.view)
使用此代码解决:
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 xCoordinateMin
声明为CGFloat
,因为它被认为是负值。然后,在创建标签时,只需使用abs()
即可获得绝对值。您现在有了一个标签,它的大小与您的工具栏按钮项目完全相同
是的,谢谢,我已经走了。。但是我想知道屏幕外边框和条形按钮项之间的距离。使用CGRectGetMinX(frame)我得到0..但是我不应该得到到主窗口的距离吗?然后你应该将
nil
传递给toView
参数到convertRect
方法。例如:buttonItemView.superview!。convertRect(buttonItemView.frame,toView:nil)
执行此操作时,帧的值是多少?你把这个方法称为哪里?调用时,按钮temview.frame
的值是多少?如果您在viewDidLoad
中执行此操作,则它不起作用。尝试在视图中执行此操作。帧为:x=-11,y=86
那么负x值是问题所在吗?
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()