Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIPopoverView background + arrow color

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"?)

enter image description here

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?

like image 602
Bernd Avatar asked Aug 20 '14 11:08

Bernd


4 Answers

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 2014.

like image 158
Bernd Avatar answered Nov 04 '22 22:11

Bernd


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)
like image 30
Daniel Kuta Avatar answered Nov 04 '22 20:11

Daniel Kuta


Seems like that popoverPresentationController.backgroundColor no longer works in iOS13.

Popover arrows now appear to take on the color of the popover viewController's view.backgroundColor.

Here's the whole code for the demo below:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let sourceButton = sender as? UIButton, let popover = segue.destination.popoverPresentationController {
        popover.sourceView = sourceButton.superview
        popover.sourceRect = sourceButton.frame
        popover.permittedArrowDirections = [.left]
        popover.delegate = self
        segue.destination.preferredContentSize = CGSize(width: 100, height: 100)
        //popover.backgroundColor = sourceButton.tintColor  //old way
        segue.destination.view.backgroundColor = sourceButton.tintColor  //new way
    }
}

@IBAction func btnTap(_ sender: Any) {
    performSegue(withIdentifier: "popoverSegue", sender: sender)
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

enter image description here

like image 4
Travis M. Avatar answered Nov 04 '22 21:11

Travis M.


SwiftUI : Xcode 11.5

Add the .background modifier with the color and add .edgesIgnoringSafeArea modifier.

.popover(isPresented: self.$vm.presentMenu, content: {
        self.menuView
           .background(Color.bgGray.edgesIgnoringSafeArea(.all))
})
like image 2
Alexander Avatar answered Nov 04 '22 21:11

Alexander