我正在尝试使用UIStackView
的内容实现Peek和Pop。问题是在浅压期间的叠加(未模糊的部分)不包含正确的内容。它位于正确的位置,因为它位于我的手指下方,但内容似乎取自视图中的其他位置:
重现的步骤:
UIStackView
添加到视图控制器的视图0
使用以下代码替换ViewController.swift
的内容:
import UIKit
class ViewController: UIViewController {
@IBOutlet var stackView: UIStackView!
override func viewDidLoad() {
super.viewDidLoad()
registerForPreviewing(with: self, sourceView: stackView)
let loremIpsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus sed feugiat ligula. Sed in rutrum lacus, vel auctor felis. Vivamus molestie felis nisi. Mauris euismod eros vitae libero commodo porttitor. Nam posuere, dui vitae aliquam mollis, quam mauris tempus turpis."
let label = UILabel()
label.numberOfLines = 0
label.text = repeatElement(loremIpsum, count: 4).joined(separator: "\n\n")
stackView.addArrangedSubview(label)
}
}
extension ViewController: UIViewControllerPreviewingDelegate {
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
previewingContext.sourceRect = CGRect(x: location.x - 50, y: location.y - 50, width: 100, height: 100)
return storyboard?.instantiateInitialViewController()
}
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
present(viewControllerToCommit, animated: true)
}
}
在(物理)设备上运行应用并强行触摸
我做错了什么,或者这是UIKit中的错误?
答案 0 :(得分:2)
更换
registerForPreviewing(with: self, sourceView: stackView)
通过
registerForPreviewing(with: self, sourceView: view)
解决了这个问题。我仍然怀疑这是一个错误,但至少这是一个解决方法。