I am placing PKCanvasView inside UIImageView. With AutoLayout, I set the size of PKCanvasView to the size of UIImageView. So far everything is working normally. PKCanvasView contentSize is equal to UIImageView’s size. However, in cases such as rotating the screen or opening the keyboard, the contentSize of PKCanvasView is not equal to the new size of UIImageView. How do I solve this?
normal
when rotation
class ExampleViewController: UIViewController {
let canvas: PKCanvasView
private var imageView: UIImageView!
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override public func viewDidLoad() {
super.viewDidLoad()
let image = Asset.example.image
imageView = UIImageView(image: image)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
imageView.clipsToBounds = false
imageView.isUserInteractionEnabled = true
canvas.tool = PKInkingTool(.pen, color: .yellow, width: 35)
canvas.drawingPolicy = .anyInput
canvas.isOpaque = true
canvas.alpha = 0.5
canvas.backgroundColor = .clear
canvas.isScrollEnabled = false
canvas.becomeFirstResponder()
canvas.minimumZoomScale = 1
canvas.maximumZoomScale = 1
canvas.translatesAutoresizingMaskIntoConstraints = false
imageView.addSubview(canvas)
view.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
imageView.topAnchor.constraint(equalTo: view.topAnchor),
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
canvas.topAnchor.constraint(equalTo: imageView.topAnchor),
canvas.leadingAnchor.constraint(equalTo: imageView.leadingAnchor),
canvas.trailingAnchor.constraint(equalTo: imageView.trailingAnchor),
canvas.bottomAnchor.constraint(equalTo: imageView.bottomAnchor),
canvas.heightAnchor.constraint(equalTo: imageView.heightAnchor),
canvas.widthAnchor.constraint(equalTo: imageView.widthAnchor)
])
}
}
New contributor
Masdsdsdqw22 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.