gpt4 book ai didi

swift - 如何设置好UIScrollView

转载 作者:行者123 更新时间:2023-11-30 10:34:56 25 4
gpt4 key购买 nike

我正在尝试设置 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
}

}

目前,我无法滚动,但我应该可以。

感谢您的帮助🙏。

最佳答案

首先,更改ViewDidLoad中的流程

override func viewDidLoad() {
super.viewDidLoad()

// Set Up the NavBar
setUpNavigationBar(button: backButton)

// Set Up Scrolling
setUpScroolView(scrooling: scrolling, autoresizing: false)
scrollingDemarcation()

// 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()

}

并将容器 View 添加到 ScrollView

// Set up container
private func setUpContainer (container : UIView, backgoundCo : UIColor, autoresizing : Bool) {
container.backgroundColor = backgoundCo
container.translatesAutoresizingMaskIntoConstraints = autoresizing
self.scrolling.addSubview(container)
}

希望这会起作用。

关于swift - 如何设置好UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com