Pārlūkot izejas kodu

setup scrollview and scrollcontainer

nayooti 5 gadi atpakaļ
vecāks
revīzija
e7b93da209
1 mainītis faili ar 157 papildinājumiem un 64 dzēšanām
  1. 157 64
      deltachat-ios/Controller/WelcomeViewController.swift

+ 157 - 64
deltachat-ios/Controller/WelcomeViewController.swift

@@ -1,25 +1,21 @@
 import UIKit
 import UIKit
 
 
-class WelcomeViewController: UIViewController, UITableViewDataSource {
+class WelcomeViewController: UIViewController {
 
 
     private lazy var scrollView: UIScrollView = {
     private lazy var scrollView: UIScrollView = {
         let scrollView = UIScrollView()
         let scrollView = UIScrollView()
-
+        scrollView.backgroundColor = .orange
+        scrollView.showsVerticalScrollIndicator = false
         return scrollView
         return scrollView
     }()
     }()
 
 
-    private lazy var tableView: UITableView = {
-        let tableView = UITableView()
-        tableView.dataSource = self
-        tableView.showsVerticalScrollIndicator = false
-        tableView.allowsSelection = false
-//        tableView.rowHeight = UITableView.automaticDimension
-//        tableView.estimatedRowHeight = UITableView.automaticDimension
-        return tableView
+    private lazy var welcomeView: WelcomeContentView = {
+        let view = WelcomeContentView()
+        view.translatesAutoresizingMaskIntoConstraints = false
+        return view
     }()
     }()
 
 
-    private let welcomeCell = WelcomeCell()
-
+    // MARK: - lifecycle
     override func viewDidLoad() {
     override func viewDidLoad() {
         super.viewDidLoad()
         super.viewDidLoad()
         setupSubviews()
         setupSubviews()
@@ -27,52 +23,162 @@ class WelcomeViewController: UIViewController, UITableViewDataSource {
 
 
     override func viewDidLayoutSubviews() {
     override func viewDidLayoutSubviews() {
         super.viewDidLayoutSubviews()
         super.viewDidLayoutSubviews()
-
-  //      welcomeCell.minumumCellHeight = view.frame.height
+        welcomeView.minContainerHeight = view.frame.height
     }
     }
 
 
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
     override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
-//         tableView.estimatedRowHeight = view.frame.height
-//        welcomeCell.minumumCellHeight = size.height
+        super.viewWillTransition(to: size, with: coordinator)
+        welcomeView.minContainerHeight = size.height
     }
     }
 
 
-    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
-        welcomeCell.minumumCellHeight = view.frame.height
-        tableView.reloadData()
+    private func setupSubviews() {
+        view.addSubview(scrollView)
+        scrollView.translatesAutoresizingMaskIntoConstraints = false
+        scrollView.addSubview(welcomeView)
 
 
-    }
+        let frameGuide = scrollView.frameLayoutGuide
+        let contentGuide = scrollView.contentLayoutGuide
 
 
-    private func setupSubviews() {
-        view.addSubview(tableView)
-        tableView.translatesAutoresizingMaskIntoConstraints = false
-        tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
-        tableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
-        tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
-        tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
-        tableView.rowHeight = view.frame.height
-    }
+        frameGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
+        frameGuide.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
+        frameGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true
+        frameGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
 
 
-    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-        return 1
-    }
+        contentGuide.leadingAnchor.constraint(equalTo: welcomeView.leadingAnchor).isActive = true
+        contentGuide.topAnchor.constraint(equalTo: welcomeView.topAnchor).isActive = true
+        contentGuide.trailingAnchor.constraint(equalTo: welcomeView.trailingAnchor).isActive = true
+        contentGuide.bottomAnchor.constraint(equalTo: welcomeView.bottomAnchor).isActive = true
 
 
-    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-        return welcomeCell
+        frameGuide.widthAnchor.constraint(equalTo: contentGuide.widthAnchor).isActive = true
     }
     }
 }
 }
 
 
 
 
-class WelcomeCell: UITableViewCell {
+class WelcomeContentView: UIView {
 
 
-    var minumumCellHeight: CGFloat = 0 {
+    var onLogin: VoidFunction?
+    var onScanQRCode: VoidFunction?
+    var onImportBackup: VoidFunction?
+
+    var minContainerHeight: CGFloat = 0 {
         didSet {
         didSet {
-            containerMinimumHeightConstraint.constant = 1000
+            containerMinHeightConstraint.constant = minContainerHeight
         }
         }
     }
     }
 
 
-    var onLogin: VoidFunction?
-    var onScanQRCode: VoidFunction?
-    var onImportBackup: VoidFunction?
+    private lazy var containerMinHeightConstraint: NSLayoutConstraint = {
+        return container.heightAnchor.constraint(greaterThanOrEqualToConstant: 0)
+    }()
+
+    private var container = UIView()
+
+    private var logoView: UIImageView = {
+        let image = #imageLiteral(resourceName: "ic_launcher").withRenderingMode(.alwaysOriginal)
+        let view = UIImageView(image: image)
+        return view
+    }()
+
+    private lazy var titleLabel: UILabel = {
+        let label = UILabel()
+        label.text = "Welcome to Delta Chat xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+        label.textColor = DcColors.grayTextColor
+        label.textAlignment = .center
+        label.numberOfLines = 0
+        label.font = UIFont.systemFont(ofSize: 100, weight: .bold)
+        return label
+    }()
+
+    private lazy var subtitleLabel: UILabel = {
+        let label = UILabel()
+        label.text = "The messenger with the broadest audience in the world. Free and independent."
+        label.font = UIFont.systemFont(ofSize: 22, weight: .regular)
+        label.textColor = DcColors.grayTextColor
+        label.numberOfLines = 0
+        label.textAlignment = .center
+        return label
+    }()
+
+    private lazy var loginButton: UIButton = {
+        let button = UIButton(type: .roundedRect)
+        let title = "log in to your server".uppercased()
+        button.setTitle(title, for: .normal)
+        button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .regular)
+        button.setTitleColor(.white, for: .normal)
+        button.backgroundColor = DcColors.primary
+        let insets = button.contentEdgeInsets
+        button.contentEdgeInsets = UIEdgeInsets(top: 8, left: 15, bottom: 8, right: 15)
+        button.layer.cornerRadius = 5
+        button.clipsToBounds = true
+        button.addTarget(self, action: #selector(loginButtonPressed(_:)), for: .touchUpInside)
+        return button
+    }()
+
+    private lazy var qrCodeButton: UIButton = {
+        let button = UIButton()
+        let title = "Scan QR code"
+        button.setTitleColor(UIColor.systemBlue, for: .normal)
+        button.setTitle(title, for: .normal)
+        button.addTarget(self, action: #selector(qrCodeButtonPressed(_:)), for: .touchUpInside)
+        return button
+    }()
+
+    private lazy var importBackupButton: UIButton = {
+        let button = UIButton()
+        let title = "Import backup"
+        button.setTitleColor(UIColor.systemBlue, for: .normal)
+        button.setTitle(title, for: .normal)
+        button.addTarget(self, action: #selector(importBackupButtonPressed(_:)), for: .touchUpInside)
+        return button
+    }()
+
+    init() {
+        super.init(frame: .zero)
+        setupSubviews()
+        backgroundColor = .white
+        container.makeBorder(color: .orange)
+    }
+
+    required init?(coder: NSCoder) {
+        fatalError("init(coder:) has not been implemented")
+    }
+
+    private func setupSubviews() {
+        addSubview(container)
+        container.translatesAutoresizingMaskIntoConstraints = false
+
+        container.topAnchor.constraint(equalTo: topAnchor).isActive = true
+        container.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
+        container.widthAnchor.constraint(equalTo: widthAnchor, multiplier: 0.75).isActive = true
+        container.centerXAnchor.constraint(equalTo: centerXAnchor, constant: 0).isActive = true
+
+        containerMinHeightConstraint.isActive = true
+
+        container.addSubview(titleLabel)
+        titleLabel.translatesAutoresizingMaskIntoConstraints = false
+        //titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
+        titleLabel.leadingAnchor.constraint(equalTo: container.leadingAnchor).isActive = true
+        titleLabel.trailingAnchor.constraint(equalTo: container.trailingAnchor).isActive = true
+        titleLabel.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
+        titleLabel.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
+
+    }
+
+    // MARK: - actions
+     @objc private func loginButtonPressed(_ sender: UIButton) {
+         onLogin?()
+     }
+
+     @objc private func qrCodeButtonPressed(_ sender: UIButton) {
+         onScanQRCode?()
+     }
+
+     @objc private func importBackupButtonPressed(_ sender: UIButton) {
+         onImportBackup?()
+     }
+
+    /*
+
+
 
 
 
 
     private let fontSize: CGFloat = 24 // probably better to make larger for ipad
     private let fontSize: CGFloat = 24 // probably better to make larger for ipad
@@ -141,9 +247,8 @@ class WelcomeCell: UITableViewCell {
 
 
 
 
     init() {
     init() {
-        super.init(style: .default, reuseIdentifier: nil)
+        super.init(frame: .zero)
         setupSubviews()
         setupSubviews()
-        contentView.makeBorder()
     }
     }
 
 
     required init?(coder: NSCoder) {
     required init?(coder: NSCoder) {
@@ -152,18 +257,18 @@ class WelcomeCell: UITableViewCell {
 
 
     private func setupSubviews() {
     private func setupSubviews() {
 
 
-        contentView.addSubview(container)
+        addSubview(container)
         container.translatesAutoresizingMaskIntoConstraints = false
         container.translatesAutoresizingMaskIntoConstraints = false
 
 
-        container.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
-        container.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
-        container.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
-        container.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
+        container.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
+        container.topAnchor.constraint(equalTo: topAnchor).isActive = true
+        container.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
+        container.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
 
 
-        containerMinimumHeightConstraint = contentView.heightAnchor.constraint(equalToConstant: 100)
+        containerMinimumHeightConstraint = heightAnchor.constraint(equalToConstant: 100)
         containerMinimumHeightConstraint.isActive = true
         containerMinimumHeightConstraint.isActive = true
 
 
-/*
+
 
 
         let verticalStackview = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
         let verticalStackview = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
         verticalStackview.axis = .vertical
         verticalStackview.axis = .vertical
@@ -184,7 +289,7 @@ class WelcomeCell: UITableViewCell {
         logoTopAnchor.priority = UILayoutPriority.defaultLow
         logoTopAnchor.priority = UILayoutPriority.defaultLow
         logoTopAnchor.isActive = true
         logoTopAnchor.isActive = true
 
 
-        /*
+
         let buttonStackview = UIStackView(arrangedSubviews: [loginButton, qrCodeButton, importBackupButton])
         let buttonStackview = UIStackView(arrangedSubviews: [loginButton, qrCodeButton, importBackupButton])
         buttonStackview.axis = .vertical
         buttonStackview.axis = .vertical
         buttonStackview.spacing = 10
         buttonStackview.spacing = 10
@@ -198,8 +303,7 @@ class WelcomeCell: UITableViewCell {
         buttonStackviewBottomAnchor.priority = .
         buttonStackviewBottomAnchor.priority = .
         buttonStackviewBottomAnchor.isActive = true
         buttonStackviewBottomAnchor.isActive = true
 
 
-        */
-        contentView.addSubview(loginButton)
+                contentView.addSubview(loginButton)
         loginButton.translatesAutoresizingMaskIntoConstraints = false
         loginButton.translatesAutoresizingMaskIntoConstraints = false
         loginButton.topAnchor.constraint(equalTo: verticalStackview.bottomAnchor, constant: 20).isActive = true
         loginButton.topAnchor.constraint(equalTo: verticalStackview.bottomAnchor, constant: 20).isActive = true
         loginButton.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
         loginButton.centerXAnchor.constraint(equalTo: contentView.centerXAnchor).isActive = true
@@ -217,19 +321,8 @@ class WelcomeCell: UITableViewCell {
         buttonStackviewBottomAnchor.priority = .defaultHigh
         buttonStackviewBottomAnchor.priority = .defaultHigh
         buttonStackviewBottomAnchor.isActive = true
         buttonStackviewBottomAnchor.isActive = true
 
 
- */
-    }
-
-    // MARK: - actions
-    @objc private func loginButtonPressed(_ sender: UIButton) {
-        onLogin?()
     }
     }
+    */
 
 
-    @objc private func qrCodeButtonPressed(_ sender: UIButton) {
-        onScanQRCode?()
-    }
 
 
-    @objc private func importBackupButtonPressed(_ sender: UIButton) {
-        onImportBackup?()
-    }
 }
 }