构建一个iOS应用,在该视图中,允许用户捏和缩放图像。 启动时,图像显示适合屏幕。 使用ImageView和ScrollView进行布局,其布局定义如下:
private func setup() {
scrollView.addSubview(imageView)
scrollView.delegate = self
view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
imageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
])
}
代码运行正常,但在切换到横向模式时会引发以下错误:
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
2020-04-08 17:19:53.006762-0700 AcvMagicEye[16556:6738323] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x28119f020 UIView:0x102bac310.height == 63.2 (active)>",
"<NSLayoutConstraint:0x2811a6bc0 UIView:0x102bac310.height == 284 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2811a6bc0 UIView:0x102bac310.height == 284 (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.
尝试注释掉一些约束,但随后导致图像缩小,而不是使图像适合显示,然后允许用户滚动和缩放。
答案 0 :(得分:0)
您忘记为imageView和scrollview添加translationsAutoresizingMaskIntoConstraints = false
class ViewController: UIViewController, UIScrollViewDelegate {
private let scrollView = UIScrollView(frame: .zero)
private let imageView = UIImageView(image: UIImage(named: "jupiter"))
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
private func setup() {
imageView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(imageView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.delegate = self
view.addSubview(scrollView)
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
imageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
])
}
}
答案 1 :(得分:0)
。
private func setup() {
scrollView.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(scrollView)
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.delegate = self
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
imageView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
imageView.topAnchor.constraint(equalTo: scrollView.topAnchor),
imageView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
])
}
}