3

I want to change the color of the arrow that UIPopoverPresentationController uses to point at its source. The only way I can see to do this is to subclass UIPopoverBackgroundView and write drawRect and draw the popover border and arrow myself. That's a pain, because there are little niceties in the default popover like rounded corners on everything. And I want to make it feel as much at home on the platform as I can.

Do I really have to draw my own popover arrow just to change the color of the arrow?

Update: I've tried setting the backgroundColor of the UIPopoverPresentationController, but it appears to change the color of everywhere the arrow might draw. I've seen it work before, but I can't nail down what makes it work when it does. See screenshot of setting the color to red. The blue is the content of the navigation controller in the popover. It's supposed to be pointing at the button.

Popover background color issue

Tom Hamming
  • 10,577
  • 11
  • 71
  • 145

1 Answers1

10

The UIPopoverPresentationController's backgroundColor property should handle that for you

Update

I've used this approach a number of times and have never seen what you have in your screenshot. Are you sure you are using popoverPresentationController.backgroundColor and not setting a different background color on a view or container? Below is screenshot of non-centered popover arrow. The view controller background is green, the popoverPresentationController.backgroundColor is red. Shown next to the code setting the value.

non-centered arrow on popover presentation controller

Update #2

I looked at the project you posted and found the problem (although I'm not entirely sure why it's the problem). You are setting the popover presentation controller's backgroundColor property inside your presented view controller under viewWillAppear:. I suspect that setting the background color like this after the presentation happened is what triggers the bug pre-iOS 10.

If you set the popover presentation controller's backgroundColor inside your presenting view controller's onPopover: method, where you are also setting the sourceView and sourceRect properties (and before you actually call presentViewController:), everything works correctly.

Daniel Hall
  • 13,457
  • 4
  • 41
  • 37
  • Wait, no. It only works if the arrow is centered. If the arrow is off-center it draws the color you set throughout the rect where the arrow would be. – Tom Hamming Aug 30 '16 at 17:11
  • And it also draws it in the corners where the content view controller's corners are rounded. – Tom Hamming Aug 30 '16 at 17:15
  • 1
    @TomHamming That's really odd... please see my updated answer. Are you setting some other properties on the popover presentation controller that might be conflicting? I've used backgroundColor a number of times without issue in many different arrow positions. – Daniel Hall Aug 31 '16 at 13:20
  • [Here](https://github.com/tomhamming/PopoverArrowColor) is my sample code on GitHub. Your solution does work like a charm on iOS 10. – Tom Hamming Aug 31 '16 at 16:27
  • I'm actually not sure it's being off-center that does it. My example project shows the issue even when the arrow is centered. – Tom Hamming Aug 31 '16 at 16:27
  • @TomHamming I think I found a fix for you, updated my answer! – Daniel Hall Aug 31 '16 at 16:46
  • 1
    That indeed is the key! Thanks. It also appears that changing after presentation is complete is safe. It must be changing it while it's presenting that causes the issue. I'd file a radar, but iOS 10 is already fine. – Tom Hamming Aug 31 '16 at 18:47
  • @TomHamming Very interesting! Thanks for that follow-up info. It may save me some hair-pulling in the future – Daniel Hall Aug 31 '16 at 19:05