Change UIPopoverView background + arrow color

2019-02-21 09:58发布

问题:

Is there a way to simply change the UIPopoverView background color (including its arrow) on iOS8?

(I did read a couple of articles on customizing "UIPopoverControllers". Does this apply here too, meaning the answer is "no"?)

Isn't this something I should be able to address in the prepareForSegue method triggering the popover? How can I reach the according view to change its appearance?

回答1:

I found the solution. Subclassing is not necessary anymore with iOS8! The background can be accessed and changed like this from within the tableview -> navigation -> popoverPresentationController

    self.navigationController?.popoverPresentationController?.backgroundColor = UIColor.redColor()

More information about this in WWDC session 214.



回答2:

You can simply modify popover like this:

    let popoverViewController = self.storyboard?.instantiateViewControllerWithIdentifier("popoverSegue")
    popoverViewController!.popoverPresentationController?.delegate = self
    popoverViewController!.modalPresentationStyle = .Popover


    let popoverSize = CGSize(width: 150, height: 60)
    popoverViewController!.preferredContentSize = popoverSize
    let popover = popoverViewController!.popoverPresentationController
    popover?.delegate = self
    popover?.permittedArrowDirections = .Up
    popover?.sourceView = self.view

    //change background color with arrow too!
    popover?.backgroundColor = UIColor.whiteColor()
    popover?.sourceRect = CGRect(x: self.view.frame.width, y: -10, width: 0, height: 0)
    presentViewController(popoverViewController!, animated: true, completion: nil)