UIPopoverBackgroundView

UIPopoverBackgroundView

我正在使用PopoverController,我想摆脱背景阴影。苹果表示要对UIPopoverBackgroundView进行子类化,并为false返回override class var wantsDefaultContentAppearance: Bool { get }
https://developer.apple.com/documentation/uikit/uipopoverbackgroundview/1619357-wantsdefaultcontentappearance

我将其分类,并将布尔值设置为false,但阴影仍然显示。如何将此子类连接到我在LogoutClass的Actionsheet中使用的PopoverController?

UIPopoverBackgroundView子类:

class PopoverBackgroundView: UIPopoverBackgroundView {

override class var wantsDefaultContentAppearance: Bool {
        get {
            return false
        }
    }
}

LogoutController:
class LogoutController:UIViewController{

fileprivate func logOff(){

let actionSheet = UIAlertController(title: nil, message: "Logging out?", preferredStyle: .actionSheet)

 let logout = UIAlertAction(title: "Log Out", style: .default){
            (action) in
//bla bla bla
}

actionSheet.addAction(logout)

if let popoverController = actionSheet.popoverPresentationController{
            popoverController.sourceView = view
            guard let window = UIApplication.shared.keyWindow else { return }
            window.backgroundColor = .clear
            popoverController.sourceRect = CGRect(x:window.bounds.midX, y:window.bounds.midY, width:0, height:0)
            popoverController.permittedArrowDirections = []

        }
present(actionSheet, animated: true, completion: nil)
}
}

最佳答案

您将必须像这样设置popoverBackgroundViewClass实例的UIPopoverPresentationController属性:

objective-c :

popoverController.popoverBackgroundViewClass = [PopoverBackgroundView class];

迅捷
popoverController?.popoverBackgroundViewClass = PopoverBackgroundView.self

根据Apple文档:

此属性的默认值为nil,这将导致演示文稿控制器使用默认的弹出窗口外观。将此属性设置为nil以外的其他值,则表示控制器将使用指定的类来绘制弹出框的背景内容。您指定的类必须是的子类
UIPopoverBackgroundView

10-07 17:50