Swift iOS - 如何将UIPopoverBackgroundView类中的方法连接到另一个类中的PopoverController?
问题描述:
我使用PopoverController,我想摆脱背景阴影。苹果表示,要继承UIPopoverBackgroundView和override class var wantsDefaultContentAppearance: Bool { get }
Swift iOS - 如何将UIPopoverBackgroundView类中的方法连接到另一个类中的PopoverController?
返回false
我子类它和布尔设置为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)
}
}
答
你必须设置你的UIPopoverPresentationController
实例的popoverBackgroundViewClass
财产,像这样:
目标C:
popoverController.popoverBackgroundViewClass = [PopoverBackgroundView class];
夫特
popoverController?.popoverBackgroundViewClass = PopoverBackgroundView.self
作为每苹果文档:
此属性的默认值是零,这将导致呈现控制器使用默认酥料饼的外观。将此属性设置为非nil值会导致表示控制器使用指定的类来绘制弹出窗口的背景内容。您指定的类必须是 UIPopoverBackgroundView 的子类。
感谢您的帮助。我尝试过,但影子仍然存在,所以我不确定它是否有效。 –
它仍然不能正常工作,但你的答案是正确的。背景类实现有问题。不过谢谢 –