Skip to content

Instantly share code, notes, and snippets.

@Haeuncs
Created August 8, 2019 05:23
Show Gist options
  • Save Haeuncs/4baeba3b67f48c676d2cbae3cc0ee4bf to your computer and use it in GitHub Desktop.
Save Haeuncs/4baeba3b67f48c676d2cbae3cc0ee4bf to your computer and use it in GitHub Desktop.
Notice ViewController With custom LargeTitle
import Foundation
import SnapKit
import RxSwift
import RxCocoa
/// 공지사항 뷰컨트롤러
class SettingNoticeViewController: UIViewController {
let disposeBag = DisposeBag()
let viewModel = SettingAnnounceViewModel()
override func viewDidLoad() {
super.viewDidLoad()
initView()
view.backgroundColor = .white
self.extendedLayoutIncludesOpaqueBars = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let customTabBarController = self.tabBarController as? TabbarViewController {
customTabBarController.hideTabBarAnimated(hide: true, completion: nil)
}
}
func initView(){
view.addSubview(navView)
view.addSubview(tableView)
// tableView.delegate = self
navView.snp.makeConstraints{ (make) in
make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
make.height.equalTo(48)
}
tableView.snp.makeConstraints{ (make) in
make.top.equalTo(navView.snp.bottom)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
}
tableView.rx.itemSelected
.subscribe(onNext: { [weak self] (indexPath) in
let vc = SettingAnnounceDetailViewController()
vc.titleLabel.text = "테스트"
vc.dateLabel.text = "\(Date())"
vc.detailLabel.text = """
navigationBar.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
make.height.equalTo(48)
}
scrollView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(navigationBar.snp.bottom)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
}
scrollContentView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(scrollView.snp.top)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
make.bottom.equalTo(scrollView.snp.bottom)
}
nameView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(scrollContentView.snp.top).offset(24)
make.left.equalTo(view.snp.left).offset(Style.padding)
make.right.equalTo(view.snp.right).offset(-Style.padding)
}
sizeCollectionView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(nameView.snp.bottom).offset(48)
make.left.equalTo(view.snp.left)
make.right.equalTo(view.snp.right)
}
addClothStackView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(sizeCollectionView.snp.bottom).offset(48)
make.left.equalTo(view.snp.left).offset(Style.padding)
make.right.equalTo(view.snp.right)
}
clothImageEditButton.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(sizeCollectionView.snp.bottom).offset(32)
make.right.equalTo(view.snp.right).offset(-24)
make.width.equalTo(48)
make.height.equalTo(48)
}
clothImageView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(addClothStackView.snp.bottom)
make.left.equalTo(view.snp.left).offset(Style.padding)
make.right.equalTo(view.snp.right).offset(-Style.padding)
make.bottom.lessThanOrEqualTo(deleteStackView.snp.top).offset(-48)
}
svgView.snp.makeConstraints{ (make) -> Void in
make.top.equalTo(addClothStackView.snp.bottom)
make.left.equalTo(view.snp.left).offset(Style.padding)
make.right.equalTo(view.snp.right).offset(-Style.padding)
make.bottom.lessThanOrEqualTo(deleteStackView.snp.top).offset(-48)
}
deleteStackView.snp.makeConstraints{ (make) -> Void in
make.centerX.equalTo(view.snp.centerX)
make.bottom.equalTo(scrollView.snp.bottom).offset(-24).priority(250)
}
"""
self?.navigationController?.pushViewController(vc, animated: true)
}).disposed(by: disposeBag)
viewModel.observable.asObservable()
.bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: SettingAnnounceCell.self)) {
row, model, cell in
cell.titleLabel.text = model.title
cell.subLabel.text = String(describing: model.subTitle)
}.disposed(by: disposeBag)
navView.dismissBtn.rx.tap
.subscribe(
onNext: { [weak self] (_) in
self?.navigationController?.popViewController(animated: true)
}).disposed(by: disposeBag)
}
lazy var titleStackView: SettingAnnounceHeaderView = {
let titleStackView = SettingAnnounceHeaderView(frame: CGRect(origin: .zero, size: CGSize(width: view.bounds.width, height: 83)))
titleStackView.translatesAutoresizingMaskIntoConstraints = false
return titleStackView
}()
lazy var tableHeaderView: UIView = {
let tableHeaderView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: view.bounds.width, height: 83)))
tableHeaderView.translatesAutoresizingMaskIntoConstraints = false
tableHeaderView.addSubview(titleStackView)
titleStackView.leadingAnchor.constraint(equalTo: tableHeaderView.leadingAnchor, constant: 16.0).isActive = true
titleStackView.topAnchor.constraint(equalTo: tableHeaderView.topAnchor).isActive = true
titleStackView.trailingAnchor.constraint(equalTo: tableHeaderView.trailingAnchor, constant: -16.0).isActive = true
titleStackView.bottomAnchor.constraint(equalTo: tableHeaderView.bottomAnchor).isActive = true
return tableHeaderView
}()
lazy var navView: navigationBarView = {
let view = navigationBarView()
view.translatesAutoresizingMaskIntoConstraints = false
view.setText = ""
view.setLeftForPop()
return view
}()
lazy var tableView: UITableView = {
let table = UITableView()
table.rowHeight = 90
table.translatesAutoresizingMaskIntoConstraints = false
table.register(SettingAnnounceCell.self, forCellReuseIdentifier: "cell")
table.backgroundColor = .white
table.tableHeaderView = tableHeaderView
return table
}()
}
extension SettingNoticeViewController: UIScrollViewDelegate, UITableViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let maxTitlePoint = tableView.convert(CGPoint(x: titleStackView.titleLabel.bounds.minX, y: titleStackView.titleLabel.bounds.maxY), from: titleStackView.titleLabel)
navView.setText = scrollView.contentOffset.y > maxTitlePoint.y ? "공지사항" : ""
}
}
class SettingAnnounceHeaderView: UIStackView {
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
axis = .horizontal
alignment = .center
addArrangedSubview(titleLabel)
// addArrangedSubview(button)
}
lazy var titleLabel: UILabel = {
let label = UILabel()
// label.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .largeTitle).pointSize, weight: .heavy)
label.text = "공지사항"
label.font = UIFont.systemFont(ofSize: 29, weight: .semibold)
label.textColor = .blackText
label.setContentHuggingPriority(.defaultLow, for: .horizontal)
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var button: UIButton = {
let buttonWidth: CGFloat = 35.0
let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: buttonWidth, height: buttonWidth)))
button.backgroundColor = .purple
button.setTitleColor(.white, for: .normal)
button.setTitle("B", for: .normal)
button.translatesAutoresizingMaskIntoConstraints = false
button.widthAnchor.constraint(equalToConstant: buttonWidth).isActive = true
button.heightAnchor.constraint(equalToConstant: buttonWidth).isActive = true
button.layer.cornerRadius = button.bounds.height * 0.5
button.layer.masksToBounds = true
return button
}()
}
class navigationBarView: UIView {
// 상세보기에서만 사용함 true -> edit mode
var isDetailEditMode: Bool = false
var _setText: String = ""
var setText: String{
get {
return _setText
}
set (new) {
titleLabel.text = new
if new == "" {
// bottomLine.backgroundColor = .clear
}else{
// bottomLine.backgroundColor = .grey03
}
}
}
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = .white
self.addSubview(titleLabel)
self.addSubview(dismissBtn)
self.addSubview(actionBtn)
NSLayoutConstraint.activate([
titleLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
titleLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor),
dismissBtn.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor),
dismissBtn.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 0),
dismissBtn.widthAnchor.constraint(equalToConstant: 48),
dismissBtn.heightAnchor.constraint(equalToConstant: 48),
actionBtn.centerYAnchor.constraint(equalTo: titleLabel.centerYAnchor),
actionBtn.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -7),
actionBtn.widthAnchor.constraint(equalToConstant: 48),
actionBtn.heightAnchor.constraint(equalToConstant: 48),
])
}
func setLeftForPop(){
dismissBtn.setImage(UIImage(named: "icnBackIOs"), for: .normal)
}
func setTitle(title: String){
titleLabel.text = title
}
func setButtonTitle(title: String){
actionBtn.setTitle(title, for: .normal)
}
func setButtonEnabled(enabled: Bool){
actionBtn.isEnabled = enabled
}
func setButtonEditText(title: String){
actionBtn.setTitle(title, for: .normal)
actionBtn.setTitleColor(.grey05, for: .normal)
}
func setButtonDoneText(title: String){
actionBtn.setTitle(title, for: .normal)
actionBtn.setTitleColor(.fitcoOrange, for: .normal)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
lazy var titleLabel : UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "레이블"
label.font = UIFont.r17
label.textColor = .blackText
return label
}()
lazy var dismissBtn : UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setImage(UIImage(named: "icnXNew"), for: .normal)
return btn
}()
lazy var actionBtn : UIButton = {
let btn = UIButton()
btn.translatesAutoresizingMaskIntoConstraints = false
btn.setTitle("", for: .normal)
btn.titleLabel?.textAlignment = .center
btn.titleLabel?.font = UIFont.r17
btn.setTitleColor(UIColor.fitcoOrange, for: .normal)
btn.setTitleColor(UIColor.grey03, for: .disabled)
return btn
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment