JSQmessageView Controller: Add “Back” Button & Ima

2019-09-09 21:29发布

I'm using JSQmessageViewController and trying to programmatically add "back" button & user image on the navigation bar. I'm using the following codes. After running it, there's no "back" button or image. Attached is the screenshot of the simulator. I tested these codes with the normal UIViewController, and they worked.

May I know why they don't work with the JSQmessageViewController? And what should I do to add the "back" button & image on the navigation bar? Thanks a lot!

    let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 64))

    navigationBar.backgroundColor = UIColor.whiteColor()
    navigationBar.delegate = self;


    let navigationItem = UINavigationItem()
    navigationItem.title = strValue

    let leftButton =  UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "back:")
    self.navigationItem.leftBarButtonItem = leftButton

    let imgButton = UIButton()

    imgButton.setImage(image, forState: .Normal)
    imgButton.addTarget(self, action: "EditProfile:", forControlEvents: UIControlEvents.TouchDown)
    imgButton.frame = CGRectMake(self.view.frame.width - 60, 0, 41, self.view.frame.height)

    var rightButton = UIBarButtonItem(customView: imgButton)

    self.navigationItem.rightBarButtonItem = rightButton


    navigationBar.items = [navigationItem]

    self.view.addSubview(navigationBar)

}

1条回答
Viruses.
2楼-- · 2019-09-09 22:16

So if you use a navigation controller to present your instantiation of the JSQMessagesViewController then the navigation bar should actually already be there. You can just provide it with those buttons and they will be in the correct place. It also seems that you may be doing things in an outdated syntax. Here is the latest Syntax.

Create a function to add your back button.

func addCancelButtonLeft() {
    let button = UIBarButtonItem(barButtonSystemItem: .back, target: self, action: #selector(dismissView))
    navigationItem.leftBarButtonItem = button
}

Create the action for the button.

func dismissView() {
    dismiss(animated: true, completion: nil)
}

Then for your Image you are actually trying to put a button in that title view. Which is fine.

func titleView() {
  let imgButton = UIButton()

  imgButton.setImage(image, forState: .Normal)
  imgButton.addTarget(self, action: #selector(EditProfile), forControlEvents: .touchUpInside)
  navigationItem.titleView = imgButton
}
 func EditProfile() {
     navigationController?.present(EditProfileViewController(), animated: true, completion: nil)
 }

let me know if you have more questions and I will see what I can do. Good luck

查看更多
登录 后发表回答