添加依赖旋转的约束后,我的UIScrollView停止工作。我将两组约束放置在两个数组中,每次旋转后,我激活其中一个约束并停用第二个约束。它可以在立即激活约束(constraint.isActive = true)之前起作用,并且只有一组。
func setupLayout() {
navigationItem.hidesBackButton = true
navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.1450980392, green: 0.1960784314, blue: 0.2509803922, alpha: 1)
setViewBackground()
let guide = self.view.safeAreaLayoutGuide
view.addSubview(scrollView)
scrollView.addSubview(contentView)
contentView.addSubview(topLeftViewContainer)
topLeftViewContainer.addSubview(idTextField)
topLeftViewContainer.addSubview(passTextField)
contentView.addSubview(bottomRightViewContainer)
bottomRightViewContainer.addSubview(logInButton)
bottomRightViewContainer.addSubview(registerButton)
// MARK: - Portrait Constraints -
//scrollView
portraitConstraints.append(scrollView.topAnchor.constraint(equalTo: guide.topAnchor))
scrollVievBottomConstraint = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: guide, attribute: .bottom, multiplier: 1.0, constant: 0)
portraitConstraints.append(scrollVievBottomConstraint)
portraitConstraints.append(scrollView.leadingAnchor.constraint(equalTo: guide.leadingAnchor))
portraitConstraints.append(scrollView.trailingAnchor.constraint(equalTo: guide.trailingAnchor))
//contentView
portraitConstraints.append(contentView.topAnchor.constraint(equalTo: scrollView.topAnchor))
portraitConstraints.append(contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor))
portraitConstraints.append(contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor))
portraitConstraints.append(contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor))
portraitConstraints.append(contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor))
//topLeftViewContainer
portraitConstraints.append(topLeftViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
portraitConstraints.append(topLeftViewContainer.heightAnchor.constraint(equalTo: guide.heightAnchor, multiplier: 0.5))
portraitConstraints.append(topLeftViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
portraitConstraints.append(topLeftViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
//idTextField
portraitConstraints.append(idTextField.heightAnchor.constraint(equalToConstant: 20))
portraitConstraints.append(idTextField.widthAnchor.constraint(equalToConstant: 180))
portraitConstraints.append(idTextField.topAnchor.constraint(equalTo: topLeftViewContainer.topAnchor, constant: 50))
portraitConstraints.append(idTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//passTextField
portraitConstraints.append(passTextField.heightAnchor.constraint(equalToConstant: 20))
portraitConstraints.append(passTextField.widthAnchor.constraint(equalToConstant: 180))
portraitConstraints.append(passTextField.topAnchor.constraint(equalTo: idTextField.bottomAnchor, constant: 20))
portraitConstraints.append(passTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//bottomRightViewContainer
portraitConstraints.append(bottomRightViewContainer.heightAnchor.constraint(equalTo: guide.heightAnchor, multiplier: 0.5))
portraitConstraints.append(bottomRightViewContainer.topAnchor.constraint(equalTo: topLeftViewContainer.bottomAnchor))
portraitConstraints.append(bottomRightViewContainer.bottomAnchor.constraint(equalTo: contentView.bottomAnchor))
portraitConstraints.append(bottomRightViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
portraitConstraints.append(bottomRightViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
//logInButton
portraitConstraints.append(logInButton.heightAnchor.constraint(equalToConstant: 30))
portraitConstraints.append(logInButton.widthAnchor.constraint(equalToConstant: 120))
portraitConstraints.append(logInButton.topAnchor.constraint(lessThanOrEqualTo: bottomRightViewContainer.topAnchor, constant: 50))
portraitConstraints.append(logInButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
//registerButton
portraitConstraints.append(registerButton.heightAnchor.constraint(equalToConstant: 30))
portraitConstraints.append(registerButton.widthAnchor.constraint(equalToConstant: 120))
portraitConstraints.append(registerButton.topAnchor.constraint(equalTo: logInButton.bottomAnchor, constant: 20))
portraitConstraints.append(registerButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
// MARK: - Landscape Constraints -
//scrollView
landscapeConstraints.append(scrollView.topAnchor.constraint(equalTo: guide.topAnchor))
scrollVievBottomConstraint = NSLayoutConstraint(item: scrollView, attribute: .bottom, relatedBy: .equal, toItem: guide, attribute: .bottom, multiplier: 1.0, constant: 0)
landscapeConstraints.append(scrollVievBottomConstraint)
landscapeConstraints.append(scrollView.leadingAnchor.constraint(equalTo: guide.leadingAnchor))
landscapeConstraints.append(scrollView.trailingAnchor.constraint(equalTo: guide.trailingAnchor))
//contentView
landscapeConstraints.append(contentView.topAnchor.constraint(equalTo: scrollView.topAnchor))
landscapeConstraints.append(contentView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor))
landscapeConstraints.append(contentView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor))
landscapeConstraints.append(contentView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor))
landscapeConstraints.append(contentView.widthAnchor.constraint(equalTo: scrollView.widthAnchor))
//topLeftViewContainer
landscapeConstraints.append(topLeftViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(topLeftViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(topLeftViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
landscapeConstraints.append(topLeftViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
//idTextField
landscapeConstraints.append(idTextField.heightAnchor.constraint(equalToConstant: 20))
landscapeConstraints.append(idTextField.widthAnchor.constraint(equalToConstant: 180))
landscapeConstraints.append(idTextField.topAnchor.constraint(equalTo: topLeftViewContainer.centerYAnchor, constant: -30))
landscapeConstraints.append(idTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//passTextField
landscapeConstraints.append(passTextField.heightAnchor.constraint(equalToConstant: 20))
landscapeConstraints.append(passTextField.widthAnchor.constraint(equalToConstant: 180))
landscapeConstraints.append(passTextField.topAnchor.constraint(equalTo: idTextField.bottomAnchor, constant: 20))
landscapeConstraints.append(passTextField.centerXAnchor.constraint(equalTo: topLeftViewContainer.centerXAnchor))
//bottomRightViewContainer
landscapeConstraints.append(bottomRightViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(bottomRightViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(bottomRightViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
landscapeConstraints.append(bottomRightViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
//logInButton
landscapeConstraints.append(logInButton.heightAnchor.constraint(equalToConstant: 30))
landscapeConstraints.append(logInButton.widthAnchor.constraint(equalToConstant: 120))
landscapeConstraints.append(logInButton.topAnchor.constraint(lessThanOrEqualTo: bottomRightViewContainer.centerYAnchor, constant: -30))
landscapeConstraints.append(logInButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
//registerButton
landscapeConstraints.append(registerButton.heightAnchor.constraint(equalToConstant: 30))
landscapeConstraints.append(registerButton.widthAnchor.constraint(equalToConstant: 120))
landscapeConstraints.append(registerButton.topAnchor.constraint(equalTo: logInButton.bottomAnchor, constant: 20))
landscapeConstraints.append(registerButton.centerXAnchor.constraint(equalTo: bottomRightViewContainer.centerXAnchor))
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: .UIKeyboardWillHide, object: nil)
NSLayoutConstraint.activate(portraitConstraints)
}
// MARK: - Keyboard Handling
@objc func keyBoardWillShow(notification: NSNotification){
let userInfo:NSDictionary = notification.userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height - view.safeAreaInsets.bottom
scrollVievBottomConstraint.constant = -keyboardHeight
scrollView.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
print("SHOW KEYBOARD")
}
@objc func keyBoardWillHide(notification: NSNotification){
scrollVievBottomConstraint.constant = 0
scrollView.setNeedsUpdateConstraints()
self.view.layoutIfNeeded()
print("HIDE KEYBOARD")
}
// MARK: - Orientation -
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
if UIDevice.current.orientation.isLandscape {
NSLayoutConstraint.deactivate(portraitConstraints)
NSLayoutConstraint.activate(landscapeConstraints)
} else {
NSLayoutConstraint.deactivate(landscapeConstraints)
NSLayoutConstraint.activate(portraitConstraints)
}
}
}
答案 0 :(得分:0)
问题可能出在以下方面:
//topLeftViewContainer
landscapeConstraints.append(topLeftViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(topLeftViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(topLeftViewContainer.leadingAnchor.constraint(equalTo: contentView.leadingAnchor))
landscapeConstraints.append(topLeftViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
guide.bottomAnchor或contentView.bottomAnchor?
// bottomRightViewContainer
landscapeConstraints.append(bottomRightViewContainer.widthAnchor.constraint(equalTo: guide.widthAnchor, multiplier: 0.5))
landscapeConstraints.append(bottomRightViewContainer.topAnchor.constraint(equalTo: contentView.topAnchor))
landscapeConstraints.append(bottomRightViewContainer.bottomAnchor.constraint(equalTo: guide.bottomAnchor))
landscapeConstraints.append(bottomRightViewContainer.trailingAnchor.constraint(equalTo: contentView.trailingAnchor))
guide.widthAnchor和guide.bottomAnchor还是contentView.widthAnchor和contentView.bottomAnchor?