Skip to content

Commit ea743a8

Browse files
HansonHanson
authored andcommitted
feat: 添加导航栏处理
1 parent b1d7ff3 commit ea743a8

18 files changed

+1015
-132
lines changed

BatChat.xcodeproj/project.pbxproj

Lines changed: 153 additions & 45 deletions
Large diffs are not rendered by default.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "universal",
5+
"scale" : "1x"
6+
},
7+
{
8+
"filename" : "nav_icon_return_black@2x.png",
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"filename" : "nav_icon_return_black@3x.png",
14+
"idiom" : "universal",
15+
"scale" : "3x"
16+
}
17+
],
18+
"info" : {
19+
"author" : "xcode",
20+
"version" : 1
21+
}
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// UserProfileViewModel.swift
3+
// BatChat
4+
//
5+
// Created by Hanson on 2021/5/27.
6+
//
7+
8+
import UIKit
9+
10+
class UserProfileViewModel: UIView {
11+
12+
/*
13+
// Only override draw() if you perform custom drawing.
14+
// An empty implementation adversely affects performance during animation.
15+
override func draw(_ rect: CGRect) {
16+
// Drawing code
17+
}
18+
*/
19+
20+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// UserProfileViewController.swift
3+
// BatChat
4+
//
5+
// Created by Hanson on 2021/5/27.
6+
//
7+
8+
import UIKit
9+
10+
class UserProfileViewController: BaseViewController {
11+
12+
lazy var statusView = UIView()
13+
lazy var bgImageView: UIImageView = {
14+
let imgView = UIImageView()
15+
imgView.image = "mine_bg_view".image
16+
17+
imgView.clipsToBounds = true
18+
imgView.layer.zPosition = -1
19+
return imgView
20+
}()
21+
// lazy var mineView = FMMineView()
22+
23+
override func viewDidLoad() {
24+
super.viewDidLoad()
25+
26+
setupUI()
27+
28+
addNavItem()
29+
}
30+
31+
func addNavItem() {
32+
33+
}
34+
35+
func setupUI() {
36+
view.backgroundColor = .backColor
37+
view.addSubview(bgImageView)
38+
// view.addSubview(mineView)
39+
bgImageView.backgroundColor = .white
40+
41+
statusView.alpha = 0
42+
bgImageView.frame = CGRect(
43+
x: 0, y: -navBottom,
44+
width: screenW,
45+
height: AdaptW(264)
46+
)
47+
// mineView.snp.makeConstraints { (make) in
48+
// make.edges.equalToSuperview().inset(UIEdgeInsets(top:
49+
// navBottom, left: 0, bottom: 0, right: 0))
50+
// }
51+
}
52+
53+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
54+
let vc = BCSessionViewController()
55+
navigationController?.pushViewController(vc, animated: true)
56+
}
57+
}
58+
59+
extension UserProfileViewController {
60+
override var preferredStatusBarStyle: UIStatusBarStyle {
61+
return .lightContent
62+
}
63+
}

BatChat/Scenes/Session/BCSessionViewController.swift

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,36 @@
88
import UIKit
99
import Chatto
1010

11-
class BCSessionViewController: BCChatViewController {
11+
class BCSessionViewController: BCChatViewController, NavigationProtocol {
12+
13+
lazy var navBar: EFNavigationBar = setupNavBar()
1214
override func viewDidLoad() {
1315
super.viewDidLoad()
14-
15-
let addButton = UIBarButtonItem(
16-
title: "Add",
17-
style: .plain,
18-
target: self,
19-
action: #selector(addRandomMessage)
20-
)
21-
22-
let removeButton = UIBarButtonItem(
23-
title: "Remove",
24-
style: .plain,
25-
target: self,
26-
action: #selector(removeRandomMessage)
27-
)
28-
29-
self.navigationItem.rightBarButtonItems = [addButton, removeButton]
16+
setupNav()
17+
18+
/// 隐藏导航栏,调整内容布局
19+
let inset = UIEdgeInsets(
20+
top: navBottom, left: 0,
21+
bottom: 0, right: 0)
22+
layoutConfiguration = ChatLayoutConfiguration(
23+
contentInsets: inset,
24+
scrollIndicatorInsets: .zero)
3025
}
3126

27+
private func setupNav() {
28+
navBar.title = "聊天信息"
29+
navBar.titleLabel.textColor = .white
30+
navBar.setRightButton(title: "添加", titleColor: .white)
31+
addNavBackColor(left: "#00B6B3".color, right: "#0FE7C3".color)
32+
navBar.cornerRadius(radius: 6, rectCorner: .bottom)
33+
34+
35+
navBar.onRightButtonClick = { [weak self] in
36+
self?.addRandomMessage()
37+
}
38+
}
39+
40+
3241
@objc
3342
private func addRandomMessage() {
3443
self.dataSource.addRandomIncomingMessage()

BatChat/Utility/AdaptViewUntil.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// AdaptViewUntil.swift
3+
// kanqiusports
4+
//
5+
// Created by Apple on 2019/12/17.
6+
// Copyright © 2019 kanqiusports. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
func AdaptW(_ viewWidth: CGFloat) -> CGFloat {
12+
return floor(AdaptViewUntil.sharedInstance.getAdaptWidth(viewWidth: viewWidth))
13+
}
14+
15+
class AdaptViewUntil: NSObject {
16+
17+
let kRefereWidth: CGFloat = 375.0
18+
19+
var _sizeRate:CGFloat
20+
21+
static let sharedInstance = AdaptViewUntil()
22+
override init() {
23+
24+
_sizeRate = UIScreen.main.bounds.size.width / kRefereWidth;
25+
}
26+
27+
func getAdaptWidth(viewWidth: CGFloat) -> CGFloat {
28+
return viewWidth * _sizeRate
29+
}
30+
31+
}

BatChat/Utility/Base/BaseViewController.swift

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import RxSwift
1111
import RxCocoa
1212
import Domain
1313

14-
class BaseViewController: UIViewController {
14+
15+
class BaseViewController: UIViewController, NavigationProtocol {
16+
17+
lazy var navBar: EFNavigationBar = setupNavBar()
18+
1519

1620
let disposeBag = DisposeBag()
17-
// MARK: - 导航
18-
var leftItem: CustomItem?
19-
var rightItem: CustomItem?
2021
/// 是否允许返回收拾
2122
var navCanBack: Bool = true {
2223
didSet {
@@ -25,51 +26,53 @@ class BaseViewController: UIViewController {
2526
}
2627
}
2728

28-
// MARK: - system
29-
override func viewDidLoad() {
30-
super.viewDidLoad()
29+
// 隐藏状态栏
30+
var isStatusBarHidden = false {
31+
didSet {
32+
self.setNeedsStatusBarAppearanceUpdate()
33+
}
34+
}
3135

32-
view.backgroundColor = .white
33-
// Do any additional setup after loading the view.
34-
automaticallyAdjustsScrollViewInsets = false
36+
override var prefersStatusBarHidden: Bool {
37+
return isStatusBarHidden
3538
}
36-
}
3739

38-
extension BaseViewController {
39-
40-
func setupNavColor(_ color: UIColor = .themeBack) {
41-
if let nav = navigationController as? NavigationController {
42-
nav.backgroud(color: color)
40+
/// 使用底部阴影
41+
open var adjustNavBarToFront = false
42+
open var navBarShowShadow: Bool = false {
43+
didSet {
44+
if navBarShowShadow {
45+
adjustNavBarToFront = true
46+
navBar.setBottomLineHidden(hidden: true)
47+
navBar.addCellShadow1()
48+
return
49+
}
50+
51+
adjustNavBarToFront = false
52+
navBar.removeCornerAndShadow()
4353
}
4454
}
45-
46-
func showLeftBar(_ imageName: String?, frame: CGRect = .zero) {
47-
if leftItem != nil {
48-
return
55+
56+
override func viewDidLoad() {
57+
super.viewDidLoad()
58+
view.backgroundColor = .white
59+
navBar.title = ""
60+
if #available(iOS 13.0, *) {
61+
modalPresentationStyle = .fullScreen
4962
}
50-
leftItem = CustomItem(imageName)
51-
navigationItem.leftBarButtonItem = leftItem
52-
5363
}
5464

55-
func showRightBar(_ imageName: String, frame: CGRect = .zero) {
56-
if rightItem != nil {
57-
return
65+
override func viewDidAppear(_ animated: Bool) {
66+
super.viewDidAppear(animated)
67+
if adjustNavBarToFront {
68+
view.bringSubviewToFront(navBar)
5869
}
59-
60-
rightItem = CustomItem(imageName)
61-
navigationItem.rightBarButtonItem = rightItem
62-
}
63-
64-
func setupHiddentNav(_ animated: Bool) {
65-
navigationController?.setNavigationBarHidden(true, animated: animated)
66-
navCanBack = false
6770
}
68-
69-
func setupShowNav(_ animated: Bool) {
70-
navigationController?.setNavigationBarHidden(false, animated: animated)
71+
}
72+
/// 所有控制器,点击空白收起键盘
73+
extension BaseViewController {
74+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
75+
super.touchesBegan(touches, with: event)
76+
view.endEditing(true)
7177
}
72-
73-
7478
}
75-

BatChat/Utility/Base/NavigationController.swift

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,54 @@
88

99
import UIKit
1010

11+
public protocol NavigationProtocol: UIViewController {
12+
var navBar: EFNavigationBar { get set }
13+
func setupNavBar() -> EFNavigationBar
14+
func addNavBackColor(left: UIColor, right: UIColor)
15+
}
16+
17+
extension NavigationProtocol {
18+
func setupNavBar() -> EFNavigationBar {
19+
let textColor: UIColor = .blackText
20+
EFNavigationBar.defaultStyle.titleColor = textColor
21+
EFNavigationBar.defaultStyle.buttonTitleColor = textColor
22+
EFNavigationBar.defaultStyle.titleFont = .pingfangMedium(17)
23+
EFNavigationBar.defaultStyle.buttonTitleFont = .pingfangMedium(15)
24+
EFNavigationBar.defaultStyle.height = navBottom
25+
26+
let navBar = EFNavigationBar.CustomNavigationBar()
27+
self.view.addSubview(navBar)
28+
self.navigationController?.navigationBar.isHidden = true
29+
navBar.barBackgroundImage = UIImage(named: "millcolorGrad")
30+
navBar.bottomLine.bottom = navBottom
31+
navBar.barBackgroundColor = .white
32+
33+
if let childCount = navigationController?.children.count,
34+
childCount > 1 {
35+
if let image = "nav_black".image {
36+
navBar.setLeftButton(image: image)
37+
}
38+
}
39+
40+
let inset = UIEdgeInsets(top: 0, left: -10, bottom: 0, right: 10)
41+
navBar.rightButton.titleEdgeInsets = inset
42+
return navBar
43+
}
44+
45+
func addNavBackColor(left: UIColor, right: UIColor) {
46+
navBar.barBackgroundColor = .clear
47+
let frame = CGRect(x: 0, y: 0, width: screenW, height: navBottom)
48+
let backLayer = UIView.buildGradientLayer(
49+
topColor: left, bottomColor: right,
50+
leftToRight: true, frame: frame)
51+
backLayer.zPosition = -1
52+
backLayer.cornerRadius = navBar.layer.cornerRadius
53+
backLayer.maskedCorners = navBar.layer.maskedCorners
54+
navBar.layer.addSublayer(backLayer)
55+
}
56+
}
57+
58+
1159
class NavigationController: UINavigationController,UINavigationControllerDelegate {
1260

1361
public var canBack = true
@@ -34,7 +82,7 @@ class NavigationController: UINavigationController,UINavigationControllerDelegat
3482
let appearance = self.navigationBar
3583
// 标题
3684
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white,
37-
NSAttributedString.Key.font:UIFont.systemFont(ofSize: 12)]
85+
NSAttributedString.Key.font:UIFont.systemFont(ofSize: 18)]
3886
// 背景
3987
appearance.setBackgroundImage(UIImage(color: .themeBack), for: UIBarMetrics.default)
4088
// 分割线

0 commit comments

Comments
 (0)