3

Small question here.

I have an UIBarButtonItem which makes a segue to another table view. When the user is done here I've implemented a unwind segue back to the first table view. Everything works fine except for the fact that the UIBarButtonItem is still selected/highlighted after the unwind segue which can be seen here. I have no idea why this is the case and was wondering how I could fix this?

Thanks in advance.

Edit: I meant this when I said it was embedded

symenize
  • 35
  • 5
  • Is the button still selected or the table row still selected? If it's the tablerow then in didSelectRowAt add self.tableView.deselectRow(at: indexPath, animated: true) – tylerSF Dec 07 '17 at 22:40
  • 1
    Possible duplicate of [Why does bar button change color?](https://stackoverflow.com/questions/47542641/why-does-bar-button-change-color) – Xcoder Dec 07 '17 at 22:42
  • See my own answer on why this is happening. – Xcoder Dec 07 '17 at 22:42
  • how is your implementation of the unwind segue? is your vc embedded in a navigation controller? – DevB2F Dec 07 '17 at 23:17
  • @Xcoder I added a picture in the link. Did this occur in your problem? I looked at your answer, but don't know how to solve a navigation leak issue myself – symenize Dec 08 '17 at 08:11
  • @DevB2F yes, is that a problem? – symenize Dec 08 '17 at 08:23
  • Look at @DevB2F's answer. It says that you should always make sure to pop the view controller when using navigation stack. – Xcoder Dec 08 '17 at 21:53
  • i have this problem on my programmatically created UIBarButtonItem button. i didn't implement an unwind it's using autocreated backbutton. – EFE Apr 25 '18 at 11:21

3 Answers3

4

It is a bug in IOS 11.2, I solved it like this:

override func viewWillDisappear(_ animated: Bool) {
    navigationController?.navigationBar.tintAdjustmentMode = .normal
    navigationController?.navigationBar.tintAdjustmentMode = .automatic
}
Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
Alex Balan
  • 61
  • 7
0

Using a navigation controller might be causing this. Using the popToViewController might solve the problem. Just replace MyViewController with the VievController you want to go to.

let vc = self.navigationController?.viewControllers

for v in vc! {

    print("viewcontroller is: \(v)")
    if v as? MyViewController != nil {
        self.navigationController?.popToViewController(v, animated: false)
    }                    
}
DevB2F
  • 4,674
  • 4
  • 36
  • 60
  • So I should create a navigation controller class? At this moment I only have the navigation controller in my storyboard. And where should I put this code? – symenize Dec 09 '17 at 13:52
  • Delete the segue you created and add an action to the UIBarButtonItem. Put this code inside the action for the button. – DevB2F Dec 09 '17 at 16:13
  • I tried it and it didn't work. I removed the segue and added an action to the barbuttonitem, ran it and although it went to the second view controller, when I came back to the first view controller the button was still selected/highlighted as in the image shown in my question. To be clear: the segue to the second view controller works, the behavior of the button occurs after the unwind segue – symenize Dec 09 '17 at 16:46
0

For anyone still trying to figure out how to solve this here is a quick work around. Go to your unwind segue method en put the following code in there:

 if self.navigationItem.rightBarButtonItem?.style == .plain {
        self.navigationItem.rightBarButtonItem?.style = .done
    }
 if self.navigationItem.rightBarButtonItem?.style == .done {
        self.navigationItem.rightBarButtonItem?.style = .plain
    }

It may not work for all cases but it worked for me. (I don't really know why it works though)

symenize
  • 35
  • 5