Search code examples
iosswiftuiscrollviewcontentoffset

Get custom view bounds from view controller


I have ViewController with multiple custom views. CustomView has an xib called customView.xib and is implemented in CustomView.swift.

CustomView has textfield, and its delegate is implemented in CustomView.swift.

When textfield inside CustomView in ViewController is tapped, delegate method:

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

function make offset is implemented in ViewController :

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

So, what I want to achieve: When some of customViews are tapped on, get their position in my scrollView, and make the scrollView offset to this value, so the view will be visible on the top of scroll.

Where i'm currently at: My bounds are zero, and I cannot pass coordinates to CustomView.swift

Any advice?


Solution

  • I tried to do @teamnorge answer. Method

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

    did all work for me. Thats all.