I have Xib file with two labels, left and right. left one has 0 on leading and right 0 on trailing. in between a >= 15 constraint. This view is used as title view in a navigation bar. My Question is: How to set this xib in order to get left label close to leftItem and right one close to the rightItem?
how the xib is called
headerVC = HeaderViewController(nibName: "HeaderViewController", bundle: nil)
how the xib is filled
navigationItem.titleView = headerVC?.view
headerVC?.lbl1.text = name
headerVC?.lbl2.text = balance
//test purpose
// headerVC?.backgroundColor = .red
//this try of mine not working
let leftWidth = self.navigationItem.leftBarButtonItem?.width ?? 0.0
let rightWidth = self.navigationItem.rightBarButtonItem?.width ?? 0.0
let sides = leftWidth + rightWidth
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
// NSLayoutConstraint.activate([
// headerVC?.view.width = screenWidth - leftWidth - rightWidth
headerVC?.view.widthAnchor.constraint(equalToConstant: screenWidth - sides).isActive = true
// ])
solution, related to the answer below and to this answer
headerVC?.backgroundColor = .red
headerVC?.view.translatesAutoresizingMaskIntoConstraints = false
let screenSize: CGRect = UIScreen.main.bounds
let screenWidth = screenSize.width
headerVC?.view.widthAnchor.constraint(equalToConstant: screenWidth * 0.75).isActive = true