Ipad上的UIView阴影未正确跨越视图的宽度

时间:2018-10-17 22:54:58

标签: ios swift ipad uiview

我在UIView上创建了一个简单的扩展,以向UIViews添加“阴影”,它在iPhone上效果很好,但在iPad上效果不佳。我无法弄清我所缺少的东西,关于如何改善/修复此问题以使阴影在iPad和iPhone上正确显示的任何想法都很棒

this._instance = await PSPDFKitWeb.load({
    pdf: props.pdfUrl,
    container: this._container,
    licenseKey: props.licenseKey,
    baseUrl: props.baseUrl,
    disableWebAssemblyStreaming: true, // <---- This here
  });

}

这是在iPad上的外观:

UIView with shadow on iPad

我是这样从UIView调用它的:

extension UIView {
func addShadow() {
    self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOpacity = 0.5
    self.layer.shadowOffset = .zero
    self.layer.shadowRadius = 10
}

}

2 个答案:

答案 0 :(得分:0)

也许您的问题在这里:

self.layer.shadowPath = UIBezierPath(rect: self.bounds).cgPath

阴影的矩形根据此处,如果self.bounds不符合实际的矩形,则可能会显示错误。建议这样写,

 self.bgView.layer.shadowPath = UIBezierPath(rect: self.bgView.bounds).cgPath
 self.bgView.layer.shadowColor = UIColor.black.cgColor
 self.bgView.layer.shadowOpacity = 0.5
 self.bgView.layer.shadowOffset = .zero
 self.bgView.layer.shadowRadius = 10

希望能为您提供帮助。

答案 1 :(得分:0)

阅读评论让我开始思考,通过稍微移动一下内容,我能够使它完美运行!谢谢大家花时间发表评论!

我保留了UIView的扩展名,但是将调用从awakeFromNib()调用移到了视图父VC的viewDidLoad()调用中。为了处理设备旋转问题,我在以下viewWillTransition(...)中添加了一个完整的代码段。

 // MARK: - Lifecycle

override func viewDidLoad() {
    super.viewDidLoad()
    guard let primaryView = primaryView as? AggregateDataViewImplementation else { return }
    primaryView.didTapClose = { [weak self] in
        self?.didTapClose?()
    }
    primaryView.tableView.delegate = self
    primaryView.tableView.dataSource = self
    primaryView.tableView.tableFooterView = UIView()

    let nib = UINib(nibName: "AggregateDataTableViewCell", bundle: nil)
    primaryView.tableView.register(nib, forCellReuseIdentifier: "AggregateDataTableViewCell")
    primaryView.addShadow()
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    guard let primaryView = primaryView as? AggregateDataViewImplementation else { return }
    primaryView.tableView.reloadData()
}

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: { (_) in
        guard let primaryView = self.primaryView as? AggregateDataViewImplementation else { return }
        primaryView.addShadow()
    }, completion: nil)

}

再次感谢!