Is not suppose AutoLayout do the all job for us once we use it. I could not determine the issue with ChatGPT, so, I nonetheless want superior folks’s assist, right here.
I need my storage view get shrink / unshrink accordingly so the principle view controller and baby controller all collectively. However As an alternative it get messy. I believe the code shall be extra self explanatory.
Any assist appreciated.
Thanks
class ViewController: UIViewController {
let tableContainer = UIView()
let childVC = StorageViewController()
override func viewDidLoad() {
tremendous.viewDidLoad()
addChild(childVC)
view.addSubview(childVC.view)
childVC.didMove(toParent: self)
tableContainer.backgroundColor = .systemIndigo
view.addSubview(tableContainer)
childVC.view.translatesAutoresizingMaskIntoConstraints = false
tableContainer.translatesAutoresizingMaskIntoConstraints = false
let topAnchor: NSLayoutYAxisAnchor
if #accessible(iOS 11.0, *) {
topAnchor = view.safeAreaLayoutGuide.topAnchor
} else {
topAnchor = topLayoutGuide.bottomAnchor
}
NSLayoutConstraint.activate([
childVC.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
childVC.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
childVC.view.topAnchor.constraint(equalTo: topAnchor),
childVC.view.heightAnchor.constraint(equalToConstant: 133),
tableContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
tableContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
tableContainer.topAnchor.constraint(equalTo: childVC.view.bottomAnchor),
tableContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor),
])
// Add button to tableContainer
let changeColorButton = UIButton(sort: .system)
changeColorButton.tintColor = .white
changeColorButton.setTitle("Change Shade", for: .regular)
changeColorButton.addTarget(self, motion: #selector(changeColorButtonTapped), for: .touchUpInside)
tableContainer.addSubview(changeColorButton)
changeColorButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
changeColorButton.centerXAnchor.constraint(equalTo: tableContainer.centerXAnchor),
changeColorButton.centerYAnchor.constraint(equalTo: tableContainer.centerYAnchor)
])
// Add Toggle Warning Container button
let toggleWarningContainerButton = UIButton(sort: .system)
toggleWarningContainerButton.tintColor = .white
toggleWarningContainerButton.setTitle("Toggle Warning", for: .regular)
toggleWarningContainerButton.addTarget(self, motion: #selector(toggleWarningContainerButtonTapped), for: .touchUpInside)
tableContainer.addSubview(toggleWarningContainerButton)
toggleWarningContainerButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
toggleWarningContainerButton.centerXAnchor.constraint(equalTo: tableContainer.centerXAnchor),
toggleWarningContainerButton.topAnchor.constraint(equalTo: changeColorButton.bottomAnchor, constant: 16)
])
}
@objc non-public func changeColorButtonTapped() {
childVC.changeStorageViewBackgroundColor(to: .pink)
}
@objc non-public func toggleWarningContainerButtonTapped() {
childVC.toggleWarningContainerVisibility()
}
}
class StorageViewController: UIViewController {
let storageView = StorageView()
override func viewDidLoad() {
tremendous.viewDidLoad()
view.backgroundColor = .yellow
// Add the customized StorageView
view.addSubview(storageView)
storageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
storageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
storageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
storageView.topAnchor.constraint(equalTo: view.topAnchor),
storageView.heightAnchor.constraint(lessThanOrEqualToConstant: 133)
])
}
func changeStorageViewBackgroundColor(to paint: UIColor) {
storageView.backgroundColor = colour
}
func toggleWarningContainerVisibility() {
storageView.toggleWarningContainerVisibility()
}
}
class StorageView: UIView {
let containerView = UIView()
let warningIconImageView = UIImageView(picture: UIImage(systemName: "exclamationmark.triangle.fill"))
let warningLabel = UILabel()
let usageLabel = UILabel()
non-public var warningContainerHeightConstraint: NSLayoutConstraint!
non-public var isWarningContainerVisible = true
override init(body: CGRect) {
tremendous.init(body: body)
setupView()
}
required init?(coder aDecoder: NSCoder) {
tremendous.init(coder: aDecoder)
setupView()
}
non-public func setupView() {
backgroundColor = .inexperienced
// Container View
containerView.backgroundColor = .yellow
addSubview(containerView)
containerView.translatesAutoresizingMaskIntoConstraints = false
warningContainerHeightConstraint = containerView.heightAnchor.constraint(lessThanOrEqualToConstant: 92)
NSLayoutConstraint.activate([
containerView.leadingAnchor.constraint(equalTo: leadingAnchor),
containerView.trailingAnchor.constraint(equalTo: trailingAnchor),
containerView.topAnchor.constraint(equalTo: topAnchor),
warningContainerHeightConstraint!,
])
// Warning Icon
containerView.addSubview(warningIconImageView)
warningIconImageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
warningIconImageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: 10),
warningIconImageView.centerYAnchor.constraint(equalTo: containerView.centerYAnchor),
warningIconImageView.widthAnchor.constraint(equalToConstant: 24),
warningIconImageView.heightAnchor.constraint(equalToConstant: 24)
])
// Warning Label
warningLabel.textual content = "storage exceed"
warningLabel.numberOfLines = 0
containerView.addSubview(warningLabel)
warningLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
warningLabel.leadingAnchor.constraint(equalTo: warningIconImageView.trailingAnchor, constant: 8),
warningLabel.centerYAnchor.constraint(equalTo: containerView.centerYAnchor)
])
// Utilization Label
usageLabel.textual content = "5GB of 4GB has been used"
addSubview(usageLabel)
usageLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
usageLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
usageLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
usageLabel.bottomAnchor.constraint(equalTo: bottomAnchor),
usageLabel.heightAnchor.constraint(lessThanOrEqualToConstant: 41)
])
}
func toggleWarningContainerVisibility() {
isWarningContainerVisible.toggle()
warningContainerHeightConstraint.fixed = isWarningContainerVisible ? 92 : 0
UIView.animate(withDuration: 0.3) {
self.containerView.isHidden = !self.isWarningContainerVisible
self.layoutIfNeeded()
}
}
}
Once I toggle visibility of warning container I count on the all view resize accordingly.