Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift present controller with a back button to get back to last screen

Tags:

xcode

swift

I am presenting a UICollectionViewController but it just ends up as a controller with no back buttons to get back to the last controller or the home controller, how would i add a back button? or what other options do i have?

class UserProfileController: UICollectionViewController, UICollectionViewDelegateFlowLayout, UserProfileHeaderDelegate, ProfileHeaderClosedDelegate {


func presentFRC() {
       let PE = FriendsRequestsController(collectionViewLayout: 
 UICollectionViewFlowLayout())
    let NPE = UINavigationController(rootViewController: PE)

    self.present(NPE, animated: true, completion: nil)
}
}

1 Answers

Add the following code in the view did load of the view controller you are presenting.

override func viewDidLoad() {
super.viewDidLoad()
var backbutton = UIButton(type: .Custom)
backbutton.setImage(UIImage(named: "BackButton.png"), forState: .Normal)
backbutton.setTitle("Back", forState: .Normal)
backbutton.setTitleColor(backbutton.tintColor, forState: .Normal) // You can change the TitleColor
backbutton.addTarget(self, action: "backAction", forControlEvents: .TouchUpInside)

self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backbutton)
}

func backAction() -> Void {        
self.navigationController?.popViewControllerAnimated(true)   
}
like image 117
Zahurafzal Mirza Avatar answered Nov 04 '25 01:11

Zahurafzal Mirza



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!