1

I'm trying to create reusable views in a xib/nib file in swift iOS 10.

When I create a stack view and have 2 objects in it and make sure my stackview is constrained to the container view of the nib/xib (top to top, bottom to bottom, leading to leading and finally trailing to trailing), I get an error saying I'm missing my Y position for the first and the second object. Creating just one of them usually fixes it. Although this is where things to go sideways. In all my previous research, I shouldn't need to do this if I have my distribution of my stackview to Fill. Although this seems to quiet Xcode, it creates a over constraint issue when I try to run my program.

Here is what I used to load my nib in a view from my main storyboard :

public protocol NibOwnerLoadable: class {
    static var nib: UINib { get }
}

//MARK:- Generic Implementation
public extension NibOwnerLoadable {
    // Use the xib file with the same name as your UIView subclass located in the bundle of that class
    static var nib: UINib {
        return UINib(nibName: String(describing: self), bundle: Bundle(for: self))
    }
}

//MARK:- Support for instation from the XIB file
public extension NibOwnerLoadable where Self: UIView {
    // Function to load content and constraints automatically
    func loadNibContent() {
        let layoutAttributes: [NSLayoutAttribute] = [.top, .leading, .bottom, .trailing]
        for view in Self.nib.instantiate(withOwner: self, options: nil) {
            if let view = view as? UIView {
                view.translatesAutoresizingMaskIntoConstraints = false
                view.frame = bounds
                self.addSubview(view)
                layoutAttributes.forEach{ attribute in self.addConstraint(NSLayoutConstraint(item: view, attribute: attribute, relatedBy: .equal, toItem: self, attribute: attribute, multiplier: 1, constant: 0.0))
                }
            }
        }
    }
}

I know my issue is an extra constraint being added by Auto Layout but why and where. This other question is very close to what I'm trying to do but for some reason my AutoLayout knowledge is a jumbled in my head. Adding a subclassed UIView to a Nib with its auto layout constraints A little help would be appreciate. Please explain the why, not just how to do it. I'm a guy who likes to understand the back story. It makes it logical and easier to retain.

Here are pictures of my filterView as a nib/xib that is loaded inside searchAndFilterView as FilterView (UIView) that is loaded inside one my view controller inside my storyboard. Think reusable Tool Views.

Storyboard ViewController

FilterView XIB

SearchAndFilterView XIB

Patrick Miron
  • 221
  • 3
  • 12
  • Let me clarify a bit more... – Patrick Miron Jun 16 '17 at 17:33
  • Let first see if I can accomplish this task first. I want my container views for my XIB/NIB view to be a certain height. These XIB/NIB Views must be a certain size and must be in a stack view so I can animate the view shrinking when this container view becomes hidden. I can do this without XIB/NIB fine but I can't accomplish this using container views subclassed of my XIB/NIB fileOwner. – Patrick Miron Jun 16 '17 at 17:39

1 Answers1

1

Woohou!

Here is how I figured this out.

I realized that my height constraints on my views using XIB/NIB are necessary to setup the view when its not hidden. But when I set the .isHidden to true value this would conflict with my height constraint.

I needed to let Autolayout do its auto layout when the view changed from hidden to not hidden and vice-versa. By setting a constraint with a Priority default value of 1000, I was telling to Autolayout to absolutely make sure this constraint is always active.

I realized that by setting the priority to 999, I told Autolayout it could override my constraint when its really needed to do it. Why 999, I assume that when a view isHidden, it really has no size and that is very high priority for Autolayout.

I wish I knew this or knew how to find out default autolayout priorities before.

If anyone knows more about this I would appreciate more info or a link!!!

Patrick Miron
  • 221
  • 3
  • 12