Skip to content

Commit 28144fb

Browse files
authored
[NL-53]: BaseViewController 구현 (#31)
* [NL-53]: Base 모듈 정의 * [NL-53]: NavigationBar 초안 * [NL-53]: BaseViewController 구현 * [NL-53]: 푸시 알림 설정 화면 BaseViewController 적용
1 parent 5651f2d commit 28144fb

File tree

11 files changed

+285
-3
lines changed

11 files changed

+285
-3
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// BaseViewController.swift
3+
// Base
4+
//
5+
// Created by ttozzi on 8/10/25.
6+
//
7+
8+
import SnapKit
9+
import UIKit
10+
11+
open class BaseViewController: UIViewController {
12+
13+
enum Constant {
14+
static let navigationBarHeight: CGFloat = 56
15+
}
16+
17+
open var navigationBarStyle: NavigationBar.Style { .text(alignment: .center) }
18+
public private(set) lazy var navigationBar = NavigationBar(style: navigationBarStyle, height: Constant.navigationBarHeight)
19+
public override var title: String? {
20+
get { navigationBar.title }
21+
set { navigationBar.title = newValue }
22+
}
23+
private var navigationAreaHeight: Constraint?
24+
25+
open override func viewDidLoad() {
26+
super.viewDidLoad()
27+
setupNavigationBar()
28+
setNavigationBarHidden(false)
29+
}
30+
31+
public func setNavigationBarHidden(_ isHidden: Bool) {
32+
navigationBar.isHidden = isHidden
33+
additionalSafeAreaInsets.top = isHidden ? .zero : Constant.navigationBarHeight
34+
}
35+
36+
public func setNavigationBarLeftButtonItems(items: [any NavigationBarItem]) {
37+
navigationBar.setLeftButtonItems(items)
38+
}
39+
40+
public func setNavigationBarRightButtonItems(items: [any NavigationBarItem]) {
41+
navigationBar.setRightButtonItems(items)
42+
}
43+
44+
private func setupNavigationBar() {
45+
view.addSubview(navigationBar)
46+
navigationBar.snp.makeConstraints { make in
47+
make.top.equalTo(view.snp.top)
48+
make.bottom.equalTo(view.safeAreaLayoutGuide.snp.top)
49+
make.horizontalEdges.equalToSuperview()
50+
}
51+
}
52+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//
2+
// NavigationBar.swift
3+
// Base
4+
//
5+
// Created by ttozzi on 8/10/25.
6+
//
7+
8+
import DesignSystem
9+
import SnapKit
10+
import Then
11+
import UIKit
12+
13+
public final class NavigationBar: UIView {
14+
15+
public enum Style {
16+
case text(alignment: NSTextAlignment = .center)
17+
case image(UIImage)
18+
}
19+
20+
private lazy var contentStackView = UIStackView().then {
21+
$0.axis = .horizontal
22+
$0.spacing = 8
23+
$0.distribution = .fill
24+
}
25+
private lazy var leftItemsSection = UIStackView().then {
26+
$0.axis = .horizontal
27+
$0.spacing = 8
28+
}
29+
private lazy var centerSection = UIStackView().then {
30+
$0.axis = .horizontal
31+
}
32+
private lazy var rightItemsSection = UIStackView().then {
33+
$0.axis = .horizontal
34+
$0.spacing = 8
35+
}
36+
private lazy var defaultTitleLabel = UILabel().then {
37+
$0.numberOfLines = 1
38+
$0.style = Typography.Body_16_B
39+
$0.textColor = STColors.gray1.color
40+
$0.textAlignment = .center
41+
$0.setContentHuggingPriority(UILayoutPriority(.zero), for: .horizontal)
42+
}
43+
private lazy var defaultImageView = UIImageView().then {
44+
$0.contentMode = .scaleAspectFit
45+
}
46+
var title: String? {
47+
get { defaultTitleLabel.text }
48+
set { defaultTitleLabel.styledText = newValue }
49+
}
50+
51+
init(style: Style, height: CGFloat) {
52+
super.init(frame: .zero)
53+
setupUI(with: style, height: height)
54+
}
55+
56+
required init?(coder: NSCoder) {
57+
fatalError("init(coder:) has not been implemented")
58+
}
59+
60+
private func setupUI(with style: Style, height: CGFloat) {
61+
addSubview(contentStackView)
62+
contentStackView.snp.makeConstraints { make in
63+
make.top.greaterThanOrEqualToSuperview()
64+
make.bottom.equalToSuperview()
65+
make.height.equalTo(height)
66+
make.horizontalEdges.equalToSuperview().inset(16)
67+
}
68+
69+
contentStackView.addArrangedSubview(leftItemsSection)
70+
contentStackView.addArrangedSubview(centerSection)
71+
centerSection.snp.makeConstraints { make in
72+
make.centerX.equalToSuperview()
73+
}
74+
centerSection.addArrangedSubview(defaultTitleLabel)
75+
centerSection.addArrangedSubview(defaultImageView)
76+
contentStackView.addArrangedSubview(rightItemsSection)
77+
78+
switch style {
79+
case .text(let alignment):
80+
defaultImageView.isHidden = true
81+
defaultTitleLabel.textAlignment = alignment
82+
83+
case .image(let image):
84+
defaultTitleLabel.isHidden = true
85+
defaultImageView.image = image.withTintColor(STColors.gray1.color)
86+
let spacer = UIView()
87+
centerSection.addArrangedSubview(spacer)
88+
}
89+
}
90+
91+
func setLeftButtonItems(_ items: [any NavigationBarItem]) {
92+
leftItemsSection.arrangedSubviews.forEach {
93+
$0.removeFromSuperview()
94+
}
95+
items.forEach {
96+
leftItemsSection.addArrangedSubview($0)
97+
}
98+
}
99+
100+
func setRightButtonItems(_ items: [any NavigationBarItem]) {
101+
rightItemsSection.arrangedSubviews.forEach {
102+
$0.removeFromSuperview()
103+
}
104+
items.forEach {
105+
rightItemsSection.addArrangedSubview($0)
106+
}
107+
}
108+
}
109+
110+
@available(iOS 17.0, *)
111+
#Preview("Text") {
112+
let navigationBar = NavigationBar(style: .text(alignment: .left), height: 56)
113+
navigationBar.snp.makeConstraints { make in
114+
make.height.equalTo(56)
115+
}
116+
navigationBar.backgroundColor = STColors.primary9.color
117+
navigationBar.title = "타이틀"
118+
navigationBar.setLeftButtonItems([NaivgationBarButtonItem.back])
119+
navigationBar.setRightButtonItems([NaivgationBarButtonItem.back])
120+
return navigationBar
121+
}
122+
123+
@available(iOS 17.0, *)
124+
#Preview("Image") {
125+
let image = STImages.navigationLogo.image
126+
let navigationBar = NavigationBar(style: .image(image), height: 56)
127+
navigationBar.snp.makeConstraints { make in
128+
make.height.equalTo(56)
129+
}
130+
navigationBar.backgroundColor = STColors.primary9.color
131+
navigationBar.setRightButtonItems([NaivgationBarButtonItem.back])
132+
return navigationBar
133+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// NavigationBarItem.swift
3+
// Base
4+
//
5+
// Created by ttozzi on 8/10/25.
6+
//
7+
8+
import DesignSystem
9+
import UIKit
10+
11+
public protocol NavigationBarItem: UIView { }
12+
13+
public final class NaivgationBarButtonItem: UIButton, NavigationBarItem {
14+
15+
public override init(frame: CGRect) {
16+
super.init(frame: frame)
17+
}
18+
19+
required init?(coder: NSCoder) {
20+
fatalError("init(coder:) has not been implemented")
21+
}
22+
23+
private func setupUI() {
24+
snp.makeConstraints { make in
25+
make.size.equalTo(24)
26+
}
27+
}
28+
}
29+
30+
public extension NaivgationBarButtonItem {
31+
static var back: NaivgationBarButtonItem {
32+
let item = NaivgationBarButtonItem()
33+
item.setImage(STImages.chevronLeftM.image, for: .normal)
34+
return item
35+
}
36+
}

Common/Base/Sources/empty.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Common/Project.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ struct CommonLayer: Layer {
1212
sources: .empty,
1313
dependencies: [
1414
.target(name: "Auth"),
15+
.target(name: "Base"),
1516
.target(name: "Constant"),
1617
.target(name: "Lib"),
1718
.project(target: "CoreLayer", path: "../Core"),
18-
.project(target: "DesignSystem", path: "../DesignSystem"),
1919
],
2020
settings: .settings(
2121
base: [
@@ -25,6 +25,14 @@ struct CommonLayer: Layer {
2525
)
2626
),
2727
.createTarget(name: "Auth"),
28+
.createTarget(
29+
name: "Base",
30+
dependencies: [
31+
.project(target: "DesignSystem", path: "../DesignSystem"),
32+
.external(name: "Then"),
33+
.external(name: "SnapKit"),
34+
]
35+
),
2836
.createTarget(name: "Constant"),
2937
.createTarget(
3038
name: "Lib",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "chevron-left-m.svg",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
Lines changed: 3 additions & 0 deletions
Loading
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "navigationLogo.svg",
5+
"idiom" : "universal"
6+
}
7+
],
8+
"info" : {
9+
"author" : "xcode",
10+
"version" : 1
11+
}
12+
}
Lines changed: 7 additions & 0 deletions
Loading

Feature/Setting/Sources/PushSetting/PushSettingViewController.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
// Created by ttozzi on 7/31/25.
66
//
77

8+
import Base
89
import Combine
910
import DesignSystem
1011
import SnapKit
1112
import Then
1213
import UIKit
1314

14-
final class PushSettingViewController: UIViewController {
15+
final class PushSettingViewController: BaseViewController {
1516

1617
private lazy var collectionView = UICollectionView(
1718
frame: .zero, collectionViewLayout: createLayout()
@@ -39,17 +40,30 @@ final class PushSettingViewController: UIViewController {
3940

4041
override func viewDidLoad() {
4142
super.viewDidLoad()
43+
setupNavigationBar()
4244
setupUI()
4345
setupBinding()
4446
viewModel.send(input: .viewDidLoad)
47+
title = "푸시알림"
48+
}
49+
50+
private func setupNavigationBar() {
51+
let backButtonItem = NaivgationBarButtonItem.back
52+
backButtonItem.tapPublisher
53+
.sink { [weak self] in
54+
self?.viewModel.send(input: .backButtonTapped)
55+
}
56+
.store(in: &cancellables)
57+
setNavigationBarLeftButtonItems(items: [backButtonItem])
4558
}
4659

4760
private func setupUI() {
4861
view.backgroundColor = STColors.white.color
4962

5063
view.addSubview(collectionView)
5164
collectionView.snp.makeConstraints { make in
52-
make.verticalEdges.equalToSuperview()
65+
make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
66+
make.bottom.equalToSuperview()
5367
make.horizontalEdges.equalToSuperview().inset(24)
5468
}
5569
}

0 commit comments

Comments
 (0)