/StackViewController.swift Secret
Last active
January 3, 2024 07:49
3.1 整体控制 ElementStackView 高度
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
import SnapKit | |
import SwiftUI | |
import Resolver | |
extension Resolver: ResolverRegistering { | |
public static func registerAllServices() { | |
register { | |
ConcreteElementGenerator() | |
}.scope(.application) | |
register { | |
AnotherElementGenerator() | |
}.scope(.application) | |
} | |
} | |
class StackViewController: UIViewController { | |
typealias EType = ElementType | |
lazy var stackView = { | |
let stackView = ElementStackView<ConcreteElementGenerator>() | |
stackView.axis = .vertical | |
stackView.distribution = .equalSpacing | |
stackView.alignment = .fill | |
stackView.backgroundColor = .lightGray.withAlphaComponent(0.1) | |
return stackView | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.backgroundColor = .white | |
let scrollView = UIScrollView() | |
view.addSubview(scrollView) | |
scrollView.snp.makeConstraints { make in | |
make.top.equalTo(view.safeAreaLayoutGuide.snp.top).offset(20).priority(.low) | |
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom).offset(-20).priority(.low) | |
make.left.equalTo(view.safeAreaLayoutGuide.snp.left).offset(20) | |
make.right.equalTo(view.safeAreaLayoutGuide.snp.right).offset(-20) | |
make.centerY.equalTo(view.safeAreaLayoutGuide.snp.centerY) | |
make.height.lessThanOrEqualTo(200) | |
} | |
scrollView.addSubview(stackView) | |
stackView.snp.makeConstraints { make in | |
make.edges.equalTo(scrollView.contentLayoutGuide.snp.edges) | |
make.width.equalToSuperview() | |
} | |
stackView.addArrangedElements(loginElementList()) | |
} | |
func loginElementList() -> [EType] { | |
return [ | |
.segment(items: ["登录", "注册"], defaultIndex: 0, onTapped: nil), | |
.spacer(height: 15), | |
.commonInput(label: "User Name: ", placeHolder: "Email/Phone/ID", onTextChanged: { text in | |
print("User Name: \(String(describing: text))") | |
}), | |
.spacer(height: 10), | |
.commonInput(label: "Password: ", placeHolder: "Password", onTextChanged: { text in | |
print("Password: \(String(describing: text))") | |
}), | |
.spacer(height: 10), | |
.checker(title: "记住用户名", checked: false, onTapped: { checked in | |
print("checked: \(checked)") | |
}), | |
.spacer(height: 10), | |
.spacer(height: 15), | |
.button(title: "登录", onTapped: nil) | |
] | |
} | |
func setupSubviews() { | |
let elementList = loginElementList() | |
stackView.addArrangedElements(elementList) | |
} | |
} | |
#Preview { | |
StackViewController() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment