9

I'm currently developing iOS apps with swift. I want to develop my apps without storyboard just with the xib files and Grouping my Views and Controllers with seperate groups. But i realized a missing thing in xib file. I can not see top layout guide and bottom layout guide in xib file. I want to arrange my UIControls according to the layout guides. But there is no one? Is there another way to set constraints with top layout guide with interface builder without using Storyboards?

Thanks in advance.

Alican Yilmaz
  • 101
  • 1
  • 2
  • Layout guide is a property of the view controller. Where's your view controller in this xib file? – matt Aug 06 '16 at 18:46
  • i do not have viewController in my xib file. Is there a way to add a viewcontroller to xib file in interface builder? – Alican Yilmaz Aug 06 '16 at 18:47
  • Of course. But that's not exactly my point. My point is that without a view controller, the question makes no sense. If you want to do layout to the layout guide in the nib, you have to have a view controller in the nib, because that's whose layout guide it is. – matt Aug 06 '16 at 18:49
  • I created a custom viewcontroller class and setted the filesowner of xib to the viewcontroller. But i saw that when i drag a viewcontroller to xib file the guides have appeared. What do you mean by have view controller in nib by drag a viewcontroller to nib? – Alican Yilmaz Aug 06 '16 at 18:52
  • "But i saw that when i drag a viewcontroller to xib file the guides have appeared" Yes, because a view controller has layout guides. This is the third time I've said it. – matt Aug 06 '16 at 18:59
  • ok but when i add vc to nib got an error like "A view can only be associated with at most one view controller at a time!" – Alican Yilmaz Aug 06 '16 at 19:08
  • I don't know how you are configuring the .xib file. It sounds like you've now got `view` outlets both from the files owner and from this new view controller; obviously that can't work. I'm just explaining why you don't see a top layout guide. If you want to see them in the .xib file, there has to be a view controller in the .xib file (fourth time). If that makes your life too difficult, you'll have to perform this configuration in a storyboard or form the constraints in code. – matt Aug 06 '16 at 19:12
  • I don't see how your question is any different from this: http://stackoverflow.com/questions/19078278/is-there-any-way-to-add-constraint-between-a-view-and-the-top-layout-guide-in-a Nothing new has magically happened to change things since then. – matt Aug 06 '16 at 19:15

3 Answers3

2

If you do not want the entire view of the ViewController to extend underneath UINavigationBar when one is present. Turn off the edgesForExtendedLayout property of the UIViewController.

self.edgesForExtendedLayout = []
1

This worked for me:

yourCustomView.topAnchor.constraint(equalTo: self.topLayoutGuide.bottomAnchor).isActive = true

from here: Is there any way to add constraint between a view and the top layout guide in a xib file?

Jay
  • 594
  • 10
  • 23
-1

When creating a ViewController class by going through the "New File" route and have "Also create XIB file" ticked, Xcode generates a xib file with the ViewController's "View" in it, hence there is no top layout guide or bottom layout guide (they come with "view controller").

If you need the layout guides, you can remove the "view" and drag a "view controller" in. Remember to set the "Class" field of the "view controller" afterwards.

xtan
  • 19
  • 3
  • I did this, but got this error instead: ('UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View is associated with", "Clear this association before associating this view with"). Are you sure you can put a view controller object inside of another view controller xib? https://stackoverflow.com/questions/13357788/a-view-can-only-be-associated-with-at-most-one-view-controller-at-a-time-uisegm – Jay Jun 30 '17 at 04:10