Alok
Alok

Reputation: 25938

Apply Auto-layout on Independent view

I have one independent view(green view) on xib and want to set height and width with auto-layout of below green view:

enter image description here

Note: I don't want to apply Auto-layout with code.

Upvotes: 0

Views: 95

Answers (3)

backslash-f
backslash-f

Reputation: 8193

You cannot do what you want without creating a relationship between your "Main View" and your custom view (your green view). Some additional steps are needed:

  1. Create a View .xib file (you already did this)
  2. Design the user interface in Xcode (it seems you already did this, kind of)
  3. Set up xib's Auto Layout constraints (in case you will add more stuff inside the green view - these are "internal" constraints)
  4. Create a Swift code file (For example: CustomView.swift, inheriting from UIView)
  5. Set .xib file’s “File’s Owner” custom class to CustomView (it must match the class name)
  6. Implement both CustomView initializers: init(coder:) and init(frame:)
  7. Load the UIView from the .xib file using the NSBundle and UINib classes
  8. Add autoresizing masks for the view to match the size of the CustomView itself
  9. Add your custom view (green view) inside the main view controller and set the desired auto-layout constraints.

Check this tutorial for a complete working example.

Upvotes: 1

lubilis
lubilis

Reputation: 4160

I think you should add constraints programmatically (or add the view in interface builder and change it's visibility but it's not good). You're trying to set constraints between green view and controller main view, but green view still not know which is its superview. That's not possible from interface builder.

Upvotes: 0

oren
oren

Reputation: 3199

you can't set constraints between views which are not related. They must be in the same view hierarchy. So I believe you can't do it in the interface builder like you want..

Upvotes: 1

Related Questions