Nuno Gonçalves
Nuno Gonçalves

Reputation: 6805

How to access custom view properties after initialize

I'm targeting IOS 8+.

I have a form that is used in more than one place. So I decided to create a custom view where I define the various "form" text fields.

I have built my XIB, and the UIView subclass contains the outlets for each textField.

The view is composed of a background image and a scroll with the form fields over it.

Now, my first obstacle was: I need to have this custom view in a container that may or may not have a navigation bar. This made me create a constraint outlet so I could update its value to push down the scroller view. This way I'd have the whole image in the frame, the top being behind the navbar and the scroller bellow the nav bar).

Here's a manual drawing to help understanding the problem. enter image description here

It's very possible that I'm making a lot of mess and confusion on my way to solve this. :)

The problem is:

After awakeFromNib runs I have no access to the constraint property. I then noticed the same thing happens for the TextFields outlets.

So, how can I access the custom view's properties when I instantiate them programatically?

Something like:

Controller:

let customView = SignupView(frame: f)
view.addSubview(customView)
customView.pushScrollerDownBy(50.0)

Custom view:

func pushScrollerDownBy(yOffset: CGFloat) {
    //topScrollerConstraint is the outlet for the textField.
    topScrollerConstraint.constant = yOffset //right now topScrollerConstraint is nil. 
}

Upvotes: 1

Views: 282

Answers (1)

Miknash
Miknash

Reputation: 7948

You should check if you have connected your topScrollerConstraint to the file's owner since it will not get instantiated and therefore, error. Here is a recent SO question regarding difference between these two:

What is File’s owner in XIB in this case?

Upvotes: 1

Related Questions