Tom Hamming Tom Hamming - 3 months ago 34
iOS Question

How to change UIPopoverPresentationController's arrow color without drawing my own

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

Answer

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.

Comments