我正在尝试设置UIScrollView,但是它不起作用。这可能是我的限制,但我不确定。此外,我想问一下我的代码是否正确。我正在从零开始学习Swift,所以任何建议都可以接受。
这是我的代码
// SecondeMenu.swift
// Bot Tes Maths
//
// Created by lucas abijmil on 23/09/2019.
// Copyright © 2019 Lucas Abijmil. All rights reserved.
//
import Foundation
import UIKit
import CoreData
class SecondeMenu: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide or not the Navigation Bar
self.navigationController?.setNavigationBarHidden(false, animated: animated)
}
override func viewDidLoad() {
super.viewDidLoad()
// Set Up the NavBar
setUpNavigationBar(button: backButton)
// Set Up the container for ChoixChapitre
setUpContainer(container: containerChoixChapitre, backgoundCo: .red, autoresizing: false)
containerChoixChapitreLabelAutoLayout()
// Set up Choix Chapitre
setUpLabelString(label: choixChapitre, text: "Choix du chapitre", textA: .center, alpha: 1, backgoundCo: .clear, autoresizing: false)
choixChapitreIntoContainer()
// Set Up Scrolling
setUpScroolView(scrooling: scrolling, autoresizing: false)
scrollingDemarcation()
}
// Declaration of our variable :
// Declaration for the backButton of the BarNavigation
private let backButton = UIBarButtonItem()
// Declaration of UIView for contain
private let containerChoixChapitre = UIView()
let test = UIView()
// Declaration of Variables for Seconde Menu (basically the name of each chapter
private let choixChapitre = UILabel()
private let scrolling = UIScrollView()
private let choixChapitreStack = UIStackView()
private let nombreReel = UILabel()
private let nombreIrationnel = UILabel()
// private let nombreReel = UILabel()
// Some functions incoming... ?? (when i'm coding)
// Set up scroolView
private func setUpScroolView (scrooling : UIScrollView, autoresizing : Bool) {
scrooling.translatesAutoresizingMaskIntoConstraints = autoresizing
view.addSubview(scrooling)
}
// Set up container
private func setUpContainer (container : UIView, backgoundCo : UIColor, autoresizing : Bool) {
container.backgroundColor = backgoundCo
container.translatesAutoresizingMaskIntoConstraints = autoresizing
view.addSubview(container)
}
// Set up NavigationBar
private func setUpNavigationBar (button : UIBarButtonItem) {
// title navigation bar
self.navigationItem.title = "Menu Seconde"
// back button set tittle
button.title = "Retour"
// Connect the button to the navigationBar
self.navigationController?.navigationBar.topItem?.backBarButtonItem = button
}
// Set Up Label
private func setUpLabelString (label : UILabel, text : String, textA : NSTextAlignment, alpha : CGFloat, backgoundCo : UIColor, autoresizing : Bool) {
label.text = text
label.textAlignment = textA
label.alpha = alpha
label.backgroundColor = backgoundCo
label.translatesAutoresizingMaskIntoConstraints = autoresizing
}
private func setUpChapitreStack (stack : UIStackView, axe : NSLayoutConstraint.Axis, space : CGFloat, autoresizing : Bool) {
stack.axis = axe
stack.spacing = space
stack.translatesAutoresizingMaskIntoConstraints = autoresizing
}
private func setUpLabelForStack (label : UILabel, text : String, textA : NSTextAlignment, alpha : CGFloat, backgroundCo : UIColor) {
label.text = text
label.textAlignment = textA
label.alpha = alpha
label.backgroundColor = backgroundCo
}
// Sizing and contraints :
// Sizing the scrolling dermarcation
private func scrollingDemarcation() {
scrolling.topAnchor.constraint(equalTo: containerChoixChapitre.bottomAnchor, constant: 20).isActive = true
scrolling.bottomAnchor.constraint(equalTo : view.bottomAnchor, constant: 80).isActive = true
scrolling.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
scrolling.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
scrolling.backgroundColor = .purple
// scrolling.addSubview(choixChapitreStack)
}
// Sizing the container for choixLabel
private func containerChoixChapitreLabelAutoLayout() {
containerChoixChapitre.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
containerChoixChapitre.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 0).isActive = true
containerChoixChapitre.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.1, constant: 0).isActive = true
containerChoixChapitre.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1, constant: 0).isActive = true
}
// Add to the scene
// Add "Choix du chapitre"
private func choixChapitreIntoContainer() {
containerChoixChapitre.addSubview(choixChapitre)
choixChapitre.heightAnchor.constraint(equalTo: containerChoixChapitre.heightAnchor, multiplier: 1, constant: 0).isActive = true
choixChapitre.widthAnchor.constraint(equalTo: containerChoixChapitre.widthAnchor, multiplier: 1, constant : 0).isActive = true
}
}
目前,我无法滚动,但我应该能够滚动。
感谢您的帮助。
答案 0 :(得分:0)
首先,更改ViewDidLoad中的流
// Set up container
private func setUpContainer (container : UIView, backgoundCo : UIColor, autoresizing : Bool) {
container.backgroundColor = backgoundCo
container.translatesAutoresizingMaskIntoConstraints = autoresizing
self.scrolling.addSubview(container)
}
并用于将容器视图添加到滚动视图
smileyFace
希望这会起作用。