Skip to content

Instantly share code, notes, and snippets.

@zaimramlan
Last active July 27, 2018 16:19
Show Gist options
  • Save zaimramlan/591b00088b2a67727d744da1399b037d to your computer and use it in GitHub Desktop.
Save zaimramlan/591b00088b2a67727d744da1399b037d to your computer and use it in GitHub Desktop.
Snippet to create a custom back button with label + image
func CustomBackButton() {
leftView = UIView()
let image : UIImage = #imageLiteral(resourceName: "back_button")
let imageView = UIImageView(frame: CGRect(x: -15, y: 0, width: 30, height: 30))
imageView.contentMode = .scaleAspectFit
imageView.image = image
leftView.addSubview(imageView)
let label: UILabel = {
let label = UILabel()
label.font = UIFont(name: "Helvetica", size: 15.0)
label.text = "Back"
label.textColor = UIColor.Blue
label.textAlignment = .right
label.sizeToFit()
label.frame = CGRect(x: 10, y: 8, width: label.frame.width, height: label.frame.height)
return label
}()
leftView.addSubview(label)
leftView.frame = CGRect(x: 0, y: 0, width: imageView.frame.width + label.frame.width, height: 30)
leftViewTapGesture.addTarget(self, action: #selector(onClickBack))
leftView.addGestureRecognizer(leftViewTapGesture)
let barButton = UIBarButtonItem(customView: leftView)
self.navigationItem.leftBarButtonItem = barButton
}
func onClickBack() {
self.navigationController?.popViewController(animated: true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment