我有带有左右两个标签的 Xib 文件。左边的前导为 0,尾随为右 0。在 >= 15 约束之间。此视图用作导航栏中的标题视图。我的问题是:如何设置此 xib 以使左标签靠近 leftItem 和右标签靠近 rightItem?
如何调用 xib
headerVC = HeaderViewController(nibName: "HeaderViewController", bundle: nil)
xib是如何填充的
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
// ])
解决方案,与下面的答案和这个答案有关
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