#ios #swift #uiscrollview #autolayout
#iOS #swift #uiscrollview #автозаполнение
Вопрос:
Я видел один и тот же вопрос снова и снова, и каждый из них получал один и тот же ответ; «Вам нужно установить значение последнего вида bottomAnchor
, равное значению ScrollView bottomAnchor
, чтобы оно расширялось».
Что ж, я это сделал, вид прокрутки прокручивается (я вижу, как перемещается полоса прокрутки), но виды не прокручиваются! Вот код;
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupConstraints()
}
fileprivate func setupConstraints() {
setupScrollViewConstraints()
setupLogoImageViewConstraints()
setupUsernameTextFieldConstraints()
setupPasswordTextFieldConstraints()
setupShowAndHideButtonConstraints()
setupForgotPasswordButtonConstraints()
setupLoginButtonConstraints()
setupAccountLabelConstraints()
let signInWithAppleButton = setupSignInWithAppleButton()
setupLoginWithFacebookButtonConstraints(signInWithAppleButton)
setupSignUpButtonConstraints()
setupTermsOfUseButtonConstraints()
}
fileprivate func setupScrollViewConstraints() {
scrollView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
}
fileprivate func setupLogoImageViewConstraints() {
logoImageView.translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 11.0, *) {
NSLayoutConstraint.activate([
logoImageView.topAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.topAnchor, constant: 16),
logoImageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
logoImageView.widthAnchor.constraint(equalToConstant: 112),
logoImageView.heightAnchor.constraint(equalToConstant: 112)
])
}
else {
NSLayoutConstraint.activate([
logoImageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16),
logoImageView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
logoImageView.widthAnchor.constraint(equalToConstant: 112),
logoImageView.heightAnchor.constraint(equalToConstant: 112)
])
}
}
fileprivate func setupUsernameTextFieldConstraints() {
usernameTextField.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
usernameTextField.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 16),
usernameTextField.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
usernameTextField.widthAnchor.constraint(equalToConstant: self.view.frame.width / 1.4),
usernameTextField.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupPasswordTextFieldConstraints() {
passwordTextField.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
passwordTextField.topAnchor.constraint(equalTo: usernameTextField.bottomAnchor, constant: 8),
passwordTextField.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
passwordTextField.widthAnchor.constraint(equalToConstant: self.view.frame.width / 1.4),
passwordTextField.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupShowAndHideButtonConstraints() {
showAndHideButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
showAndHideButton.leadingAnchor.constraint(equalTo: passwordTextField.trailingAnchor, constant: -30),
showAndHideButton.trailingAnchor.constraint(equalTo: passwordTextField.trailingAnchor),
showAndHideButton.centerYAnchor.constraint(equalTo: passwordTextField.centerYAnchor),
showAndHideButton.heightAnchor.constraint(equalToConstant: 20)
])
}
fileprivate func setupForgotPasswordButtonConstraints() {
forgotPasswordButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
forgotPasswordButton.topAnchor.constraint(equalTo: passwordTextField.bottomAnchor),
forgotPasswordButton.leadingAnchor.constraint(equalTo: passwordTextField.leadingAnchor),
forgotPasswordButton.trailingAnchor.constraint(equalTo: passwordTextField.trailingAnchor),
forgotPasswordButton.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupLoginButtonConstraints() {
loginButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
loginButton.topAnchor.constraint(equalTo: forgotPasswordButton.bottomAnchor, constant: 20),
loginButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
loginButton.widthAnchor.constraint(equalToConstant: 210),
loginButton.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupAccountLabelConstraints() {
accountLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
accountLabel.topAnchor.constraint(equalTo: loginButton.bottomAnchor, constant: 40),
accountLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
accountLabel.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupSignInWithAppleButton() -> UIControl? {
if #available(iOS 13.0, *) {
let signInWithAppleButton = ASAuthorizationAppleIDButton(type: .default, style: .white)
signInWithAppleButton.addTarget(self, action: #selector(loginWithApple(_:)), for: .touchUpInside)
signInWithAppleButton.cornerRadius = 12
scrollView.addSubview(signInWithAppleButton)
signInWithAppleButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
signInWithAppleButton.topAnchor.constraint(equalTo: accountLabel.bottomAnchor, constant: 8),
signInWithAppleButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
signInWithAppleButton.widthAnchor.constraint(equalToConstant: 210),
signInWithAppleButton.heightAnchor.constraint(equalToConstant: 45)
])
return signInWithAppleButton
}
return nil
}
fileprivate func setupLoginWithFacebookButtonConstraints(_ signInAppleButton: UIControl?) {
facebookButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
facebookButton.topAnchor.constraint(equalTo: signInAppleButton?.bottomAnchor ?? accountLabel.bottomAnchor, constant: 8),
facebookButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
facebookButton.widthAnchor.constraint(equalToConstant: 210),
facebookButton.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupSignUpButtonConstraints() {
signUpButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
signUpButton.topAnchor.constraint(equalTo: facebookButton.bottomAnchor, constant: 8),
signUpButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
signUpButton.widthAnchor.constraint(equalToConstant: 210),
signUpButton.heightAnchor.constraint(equalToConstant: 45)
])
}
fileprivate func setupTermsOfUseButtonConstraints() {
termsButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
termsButton.topAnchor.constraint(equalTo: signUpButton.bottomAnchor, constant: 8),
termsButton.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
termsButton.widthAnchor.constraint(equalToConstant: 210),
termsButton.heightAnchor.constraint(equalToConstant: 50),
termsButton.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor)
])
}
Эта проблема сводит меня с ума уже несколько дней! Чего мне не хватает?
Ответ №1:
Ваш logoImageView
ограничен неправильным местом…
// constrain Top to scrollView contentLayoutGuide Top
//logoImageView.topAnchor.constraint(equalTo: scrollView.safeAreaLayoutGuide.topAnchor, constant: 16),
logoImageView.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor, constant: 16),
Комментарии:
1. Чувак, ты меня спас!!! Не могли бы вы объяснить, почему это работает?
2. Когда вы ссылаетесь на вид прокрутки,
safeAreaLayoutGuide
вы ссылаетесь на рамку вида прокрутки… это не то, что вы хотите.