-
Notifications
You must be signed in to change notification settings - Fork 0
[#26] HandyNavigation 추가 #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // | ||
| // NavigationViewController.swift | ||
| // Handy-Storybook | ||
| // | ||
| // Created by 서준영 on 11/13/24. | ||
| // | ||
|
|
||
| import UIKit | ||
| import Handy | ||
| import SnapKit | ||
|
|
||
| class NavigationViewController: HandyNavigation { | ||
|
|
||
| let page1: UIViewController = { | ||
| let viewController = UIViewController() | ||
| viewController.view.backgroundColor = HandySemantic.bgStatusPositive | ||
| viewController.tabBarItem = UITabBarItem(title: "Label", image: HandyIcon.icHomeFilled, selectedImage: HandyIcon.icHomeFilled) | ||
| return viewController | ||
| }() | ||
|
|
||
| let page2: UIViewController = { | ||
| let viewController = UIViewController() | ||
| viewController.view.backgroundColor = HandySemantic.bgBasicBlack | ||
| viewController.tabBarItem = UITabBarItem(title: "Label", image: HandyIcon.icHomeFilled, selectedImage: HandyIcon.icHomeFilled) | ||
| return viewController | ||
| }() | ||
|
|
||
| let page3: UIViewController = { | ||
| let viewController = UIViewController() | ||
| viewController.view.backgroundColor = HandySemantic.bgStatusNegative | ||
| viewController.tabBarItem = UITabBarItem(title: "Label", image: HandyIcon.icHomeFilled, selectedImage: HandyIcon.icHomeFilled) | ||
| return viewController | ||
| }() | ||
|
|
||
| let page4: UIViewController = { | ||
| let viewController = UIViewController() | ||
| viewController.view.backgroundColor = HandySemantic.bgBasicDefault | ||
| viewController.tabBarItem = UITabBarItem(title: "Label", image: HandyIcon.icHomeFilled, selectedImage: HandyIcon.icHomeFilled) | ||
| return viewController | ||
| }() | ||
|
|
||
| let page5: UIViewController = { | ||
| let viewController = UIViewController() | ||
| viewController.view.backgroundColor = HandySemantic.iconBasicSecondary | ||
| viewController.tabBarItem = UITabBarItem(title: "Label", image: HandyIcon.icHomeFilled, selectedImage: HandyIcon.icHomeFilled) | ||
| return viewController | ||
| }() | ||
|
|
||
| override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| setViewControllers([page1, page2, page3, page4, page5], animated: false) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| // | ||
| // HandyNavigation.swift | ||
| // Handy | ||
| // | ||
| // Created by 서준영 on 11/7/24. | ||
| // | ||
|
|
||
| import UIKit | ||
|
|
||
| open class HandyNavigation: UITabBarController { | ||
|
|
||
| /// 내부에서 사용되는 레이아웃 수치입니다. | ||
| let itemImageVerticalInset: CGFloat = 10 | ||
|
|
||
| /// 상단 탭바에 사용되는 디바이더입니다. | ||
| let divider = HandyDivider() | ||
|
|
||
| private let customTabBar = CustomTabBar() | ||
|
|
||
| open override func viewDidLoad() { | ||
| super.viewDidLoad() | ||
| setupView() | ||
| } | ||
|
|
||
| /// 뷰 세팅 | ||
| private func setupView() { | ||
| setValue(customTabBar, forKey: "tabBar") | ||
| setProperties() | ||
| setLayouts() | ||
| } | ||
|
|
||
| /// 프로퍼티 세팅 | ||
| private func setProperties() { | ||
| let appearance = UITabBarAppearance() | ||
| appearance.configureWithOpaqueBackground() // 불투명한 배경으로 설정 | ||
| appearance.backgroundColor = HandySemantic.bgBasicDefault | ||
| appearance.shadowColor = .clear // 탭바 그림자 제거 | ||
|
|
||
| // 탭바 아이템 설정 | ||
| /// 기본 설정 | ||
| let tabBarItemAppearance = UITabBarItemAppearance(style: .stacked) | ||
| tabBarItemAppearance.normal.iconColor = HandySemantic.iconBasicDisabled // 아이콘 색상 | ||
| tabBarItemAppearance.normal.titleTextAttributes = [ | ||
| .foregroundColor: HandySemantic.textBasicDisabled, // 텍스트 색상 | ||
| .font: UIFont.systemFont(ofSize: 11)] // 텍스트 폰트 사이즈 | ||
|
||
|
|
||
| /// 선택 되었을 때 설정 | ||
| tabBarItemAppearance.selected.iconColor = HandySemantic.iconBasicPrimary // 선택된 아이콘 색상 | ||
| tabBarItemAppearance.selected.titleTextAttributes = [ | ||
| .foregroundColor: HandySemantic.textBasicPrimary, // 선택된 텍스트 색상 | ||
| .font: UIFont.systemFont(ofSize: 11)] // 텍스트 폰트 사이즈 | ||
|
|
||
| // appearance의 각 레이아웃 스타일에 탭바 아이템 외형 설정을 적용 | ||
| appearance.stackedLayoutAppearance = tabBarItemAppearance | ||
| appearance.compactInlineLayoutAppearance = tabBarItemAppearance | ||
| appearance.inlineLayoutAppearance = tabBarItemAppearance | ||
|
|
||
| tabBar.standardAppearance = appearance | ||
| tabBar.scrollEdgeAppearance = appearance | ||
| } | ||
|
|
||
| /// 레이아웃 세팅 | ||
| private func setLayouts() { | ||
| tabBar.addSubview(divider) | ||
| divider.snp.makeConstraints { | ||
| $0.top.leading.trailing.equalToSuperview() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| //MARK: - 탭바 아이템 중앙 정렬 | ||
| extension HandyNavigation { | ||
| open override var viewControllers: [UIViewController]? { | ||
| didSet { setTabBarItemImageInsets() } | ||
| } | ||
|
|
||
| open override func setViewControllers(_ viewControllers: [UIViewController]?, animated: Bool) { | ||
| super.setViewControllers(viewControllers, animated: true) | ||
| setTabBarItemImageInsets() | ||
| } | ||
|
|
||
| /// title이 nil이라면 imageInsets을 조정해서 image를 중앙 정렬합니다. | ||
| public func setTabBarItemImageInsets() { | ||
| viewControllers?.forEach { | ||
| if $0.tabBarItem.title == nil { | ||
| // title이 없는 경우 | ||
| $0.tabBarItem.imageInsets = UIEdgeInsets(top: itemImageVerticalInset, left: 0, bottom: -itemImageVerticalInset, right: 0) | ||
| } else { | ||
| tabBarItem.imageInsets = UIEdgeInsets( | ||
| top: itemImageVerticalInset, | ||
| left: 0, | ||
| bottom: 0, | ||
| right: 0 | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
||
|
|
||
| //MARK: - 네비게이션 바 레이아웃 설정 (Safe Area 처리) | ||
| class CustomTabBar: UITabBar { | ||
| let fixedHeight: CGFloat = 56 | ||
|
|
||
| override func layoutSubviews() { | ||
| super.layoutSubviews() | ||
|
|
||
| // Safe Area를 고려한 높이 설정 | ||
| var newFrame = self.frame | ||
| let safeAreaInsets: CGFloat = { | ||
| if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene, | ||
| let window = windowScene.windows.first { | ||
| return window.safeAreaInsets.bottom | ||
| } | ||
| return 0 | ||
| }() | ||
| newFrame.size.height = fixedHeight + safeAreaInsets | ||
| newFrame.origin.y = UIScreen.main.bounds.height - newFrame.size.height | ||
| self.frame = newFrame | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "info" : { | ||
| "author" : "xcode", | ||
| "version" : 1 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "images" : [ | ||
| { | ||
| "filename" : "icHome.png", | ||
| "idiom" : "universal", | ||
| "scale" : "1x" | ||
| }, | ||
| { | ||
| "filename" : "[email protected]", | ||
| "idiom" : "universal", | ||
| "scale" : "2x" | ||
| }, | ||
| { | ||
| "filename" : "[email protected]", | ||
| "idiom" : "universal", | ||
| "scale" : "3x" | ||
| } | ||
| ], | ||
| "info" : { | ||
| "author" : "xcode", | ||
| "version" : 1 | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tabBar의 appearance를 바꾸지 않고 UITabBarController의 tabBar 요소를 직접 갈아끼우는 방식을 사용하신 이유가 궁금합니다!
단순 사이즈만 조정하고 싶으신거라면, UITabBar의 사이즈를 조절하는 함수를 override 하는 방식으로도 가능할 것 같아서요! (아마 sizeThatFits일꺼에요)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
굳이 UITabBarController의 tabBar를 customTabBar로 갈아끼워서 높이를 조절할 필요는 없다고 생각하여 UITabBarControllerdml tabBar의 높이를 조절하는 것으로 코드를 수정했습니다!