遇到一些变态需求,UI不喜欢系统默认的间距。所以研究下修改rightBarButtonItem与leftBarButtonItem边距的问题。
代码如下:
let rightBtn = UIButton(frame: CGRect(x: 5, y: 20, width: 60, height: 44))
rightBtn.setTitle("测试", for: .normal)
rightBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
rightBtn.contentHorizontalAlignment = .right
let rightBarButton = UIBarButtonItem(customView: rightBtn)
let spaceBar = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
spaceBar.width = -15
self.navigationItem.rightBarButtonItems = [spaceBar,rightBarButton]
效果如下:
大功告成。。。