向右移动导航栏

I enter image description herehave this login page and I want to be able to modify the back button. I want to move the back button to the right, I tried the CGRect command but it didnt work. How can I move this arrow to the right. Thank you.

var layoutGuide:UILayoutGuide!
func addBackButton() {


    if #available(iOS 11.0, *) {
        layoutGuide = view.safeAreaLayoutGuide
    } else {
        // Fallback on earlier versions
        layoutGuide = view.layoutMarginsGuide
    }
    let backButton = UIButton(type: .custom)
    backButton.setImage(UIImage(named: "arrow.png"), for: .normal) // Image can be downloaded from here below link
    backButton.setTitleColor(backButton.tintColor, for: .normal) // You can change the TitleColor
    backButton.tintColor = blueColors
    backButton.frame = CGRect(x: 40, y: 0, width: 30, height: 30)
    backButton.translatesAutoresizingMaskIntoConstraints = false
    backButton.addTarget(self, action: #selector(self.backAction(_:)), for: .touchUpInside)
    self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backButton)


}
@objc func backAction(_ sender:UIButton) -> Void {
    self.navigationController?.popViewController(animated: true)
}
评论
  • ysunt
    ysunt 回复

    要将此按钮保持在中间,请执行以下操作:

    self.navigationItem.titleView = backButton;
    

    将此按钮保持在右侧:

    self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: backButton)
    
  • 坏pi气
    坏pi气 回复

    如果您想要右侧的按钮,只需将self.navigationItem.leftBarButtonItem更改为self.navigationItem.rightBarButtonItem。

    否则,由于导航栏中没有其他内容,因此完全摆脱导航栏,然后在想要后退按钮的位置添加一个普通按钮。