It's my understanding that with iOS 7, 8 and 9, when you present a popover in your application, all UIBarButtonItems in a UIToolbar are supposed to automatically be grayed out.
However, most of the time what I get is that only the button you tapped turns gray, and the other buttons remain in their original color.
I built a simple test app with a story board that has:
In code, for the UIViewController that presents the popover I have just this:
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowPopover" {
let popoverViewController = segue.destinationViewController
popoverViewController.preferredContentSize = CGSizeMake(320.0, 224.0)
if let popoverController = popoverViewController.popoverPresentationController {
// set the delegate, so adaptivePresentationStyleForPresentationController is called
popoverController.delegate = self
}
}
}
// MARK: - UIAdaptivePresentationControllerDelegate
// return .None to show as a popover on iPhone too
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
return .None
}
}
Even with that simple example, when I tap one of the buttons to present the popover, only the tapped button turns gray.
Am I misunderstanding the way UIBarButtonItems are supposed to behave when a popover is presented? Or is this a known iOS SDK bug? Are there any workarounds?
Update: I've posted a sample application here that demonstrates the problem, using the iOS 8.4 SDK: https://dl.dropboxusercontent.com/u/2349787/BarButtonPopover.zip
The buttons should all be gray when the popover appears (at least on a white/light gray toolbar) - as far as I can tell it's a tintColor change.
It's hard to tell what it should be. Take a look at Mail on the iPad. It has the very same behavior your app demonstrates: pressed icon and all other bars become dimmed. While I don't agree that this looks good, it apparently is what Apple wants.
I've also tried changing the UIViewTintAdjustmentMode of the toolbar to .Dimmed when the popover is presented, but that doesn't help either.
I had the very same idea. I swizzled -setTintAdjustmentMode:
to find out what Apple is doing behind the scenes, and it appears to be some very crazy shit. They force the remaining buttons in the bar to be tinted by setting the tint adjustment mode to normal instead of automatic. Hence setting the bar itself to dimmed doesn't show any effect, because the buttons do not inherit the tint adjustment mode.
I would just stick with how it is. Maybe Apple will change the behavior in a future release - and you will always be up to date. If you really want all buttons to be dimmed, you should iterate through all subviews of the bar and set their tintAdjustmentMode
to automatic. However, I have not figured out when to do this since Apple does not provide an appropriate hook. You will need to do some -performSelector:afterDelay:0.0
(which just highlights how hacky it is) on some hooks. Programmatic dismissal doesn't even offer any hook at all. Also Apple seems to reconfigure the tintAdjustmentMode
s upon rotation... There is too much to consider for it to be worth the time in my opinion.