Linked Questions

Popular Questions

Loading UIView into rightBarButtonItem from a nib file

Asked by At

I am trying to load a uiview xib to my UINavigationBar right item, the view loads fine but all the element inside of the view like buttons/switch they don't work and they are not even clickable and act as an still image.

class MyView: UIView {
@IBOutlet private var view: UIView!

required init() {
    self.init()
    commonInit()
}

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}

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

private func commonInit() {
    Bundle.main.loadNibNamed("MyView", owner: self, options: nil)
    addSubview(buttonContainerView)
    view.frame = self.bounds
    view.autoresizingMask = [.flexibleHeight, .flexibleHeight]
}

inside my viewDidLoad of the VC that I want to put this view on the nav item:

let button = UIBarButtonItem(customView: MyView())
navigationItem.rightBarButtonItem = button

what is the best way? If I create my view programmatically it works fine. however this way it doesn't work as expected.

Related Questions