swift ios11 修改rightBarButtonItems之间的间距

        let settingBtn = UIButton()
        settingBtn.frame = CGRect(x: 0, y: 0, width: 22, height: 20)
        settingBtn.contentMode = .scaleAspectFit
        settingBtn.setImage(UIImage(named: "个人中心设置"), for: .normal)
        settingBtn.addTarget(self, action: #selector(settingEvent), for: .touchUpInside)

        let settingView = UIView(frame: settingBtn.frame)
        settingView.addSubview(settingBtn)
        let settingBarItem = UIBarButtonItem(customView: settingView)
        

        let feedBackBtn = UIButton()
        feedBackBtn.frame = CGRect(x: 0, y: 0, width: 20, height: 22)
        feedBackBtn.contentMode = .scaleAspectFit
        feedBackBtn.setImage(UIImage(named: "个人中心反馈"), for: .normal)
        feedBackBtn.addTarget(self, action: #selector(feedBackEvent), for: .touchUpInside)
        
        let feedBackView = UIView(frame: feedBackBtn.frame)
        feedBackView.addSubview(feedBackBtn)
        let feedBackBarItem = UIBarButtonItem(customView: feedBackView)
        navigationItem.rightBarButtonItems = [feedBackBarItem, settingBarItem]
        
        let tempBarItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        tempBarItem.width = 25
        navigationItem.rightBarButtonItems = [feedBackBarItem, tempBarItem, settingBarItem]
  • UIBarButtonItem.init(customView: view)这种方式如果直接将Button放进去,那么你设置的frame的位置是无效的

  • let tempBarItem = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)

  • tempBarItem.width = 25

站位一个item大小的距离来进行调整间距就可以了

image.png

你可能感兴趣的:(swift ios11 修改rightBarButtonItems之间的间距)