我有一个带有两个标签的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
headerVC?.view.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
headerVC?.view.leadingAnchor.constraint(equalTo: navigationItem.leftBarButtonItem?.customView?.leadingAnchor, constant: 0)
])
您的约束将标题视图配置为尽可能小,同时仍显示标签。如果希望标题视图更宽,则必须添加宽度约束以使其更宽。标题视图无法以任何方式“查看”导航栏中的左右项目。您只需要自己确定宽度是多少,然后进行设置即可。