Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

ios - IB Designables: Failed to render and update auto layout status

I have a custom view (xib) that has a UIButton inside of it, I made id IBDesignable doing the following:

UserView.swift

import UIKit

@IBDesignable
class UserView: UIView {

    @IBOutlet var view: UIView!
    @IBOutlet weak var userButton: UIButton!

    override init(frame: CGRect) {
        super.init(frame: frame)
        load()

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        load()
    }

    fileprivate func load() {
        Bundle.main.loadNibNamed("UserView", owner: self, options: nil)
        addSubview(view)
        self.frame = bounds
    }

}

UserView.xib

  • Added the UIButton and set constraints
  • Set the File's Owner: UserView

Storyboard

I added a UIView to a Bar Button Item and assigned UserView class but nothing is rendering, and I got the following build error:

error: IB Designables: Failed to render and update auto layout status for FoodViewController (bR3-Kz-wX7): The agent threw an exception.

My currently environment: Xcode 9, Swift 4

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just had the exact same issue, and the only thing that worked for me was instead of using the main Bundle I had to get the Bundle for the Nib I wanted. Essentially changing:

Bundle.main.loadNibNamed("UserView", owner: self, options: nil)

To:

let bundle = Bundle(for: UserView.self)
bundle.loadNibNamed("UserView", owner: self, options: nil)

Which is bizarre, as in my case when comparing the two Bundles at runtime in LLDB they were identical (class name is different but the rest of my setup was identical to the OP)

lldb screenshot Updated

Why loading NIB from main bundle does not work?

Because when InterfaceBuilder is rendering it is not running the application. There is no "main application bundle".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...