从视图控制器获取自定义视图边界

Get custom view bounds from view controller

ViewController 有多个自定义视图。 CustomView 有一个名为 customView.xib 的 xib,并在 CustomView.swift 中实现。

CustomView有textfield,它的delegate在CustomView.swift.

中实现

当点击 ViewControllerCustomView 内的文本字段时,委托方法:

    func textFieldDidBeginEditing(textField: UITextField) {
        var field = textField as! AddItemTextField
         ViewController?.makeViewOffset(self.bounds.origin.y - 20)          
        }
    }

函数 make offset 在 ViewController 中实现:

func makeViewOffset(offset: CGFloat) {
    UIView.animateWithDuration(0.5, animations: { () -> Void in
        self.scrollView.contentOffset.y = offset
    })
}

所以,我想实现的是:当一些自定义视图被点击时,获取它们在我的滚动视图中的位置,并使滚动视图偏移到这个值,这样视图就会在滚动顶部可见。

我目前所在的位置:我的边界为零,我无法将坐标传递给 CustomView.swift

有什么建议吗?

我试着做@teamnorge 的回答。 方法

func convertRect(rect: CGRect, fromView view: UIView?) -> CGRect

对我有用。就这些了。