Skip to content

Commit

Permalink
1.创建tabbar
Browse files Browse the repository at this point in the history
  • Loading branch information
Caffe-del committed Sep 4, 2020
1 parent 5886438 commit 764a0f2
Show file tree
Hide file tree
Showing 13 changed files with 174 additions and 451 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ playground.xcworkspace
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
*.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
Expand Down
381 changes: 0 additions & 381 deletions BottomBar.xcodeproj/project.pbxproj

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion BottomBar/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let tabBarVC = RootTabBarController.init()
let tabBarVC = RootTabBarController()
self.window?.rootViewController = tabBarVC
self.window?.makeKeyAndVisible()
return true
Expand Down
4 changes: 2 additions & 2 deletions BottomBar/MusicDetailVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class MusicDetailVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .systemBlue;
// Do any additional setup after loading the view.
}

override func viewWillAppear(_ animated: Bool) {
if let tabBarController = self.tabBarController as? RootTabBarController {
tabBarController.updateBottomStyle(.miniPlayViewOnly)
tabBarController.updateBottomStyle(.none)
}
}
}
4 changes: 2 additions & 2 deletions BottomBar/MusicViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ class MusicViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

self.view.backgroundColor = .white
// Do any additional setup after loading the view.
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let vc = MusicDetailVC.init()
let vc = MusicDetailVC()
self.navigationController?.pushViewController(vc, animated: true)
}

Expand Down
31 changes: 29 additions & 2 deletions BottomBar/RootTabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@
// RootTabBar.swift
// BottomBar
//
// Created by 全达晖 on 2020/8/23.
// Created by 全达晖 on 2020/9/3.
// Copyright © 2020 全达晖. All rights reserved.
//

import UIKit

class RootTabBar: UITabBar {


var itemsContainer: UIView?
var tabBarItems = [RootTabBarItem]()

override func layoutSubviews() {
super.layoutSubviews()
for tempView in self.subviews {
if (tempView.isKind(of: NSClassFromString("UITabBarButton")!)) {
tempView.removeFromSuperview()
}
}

self.layoutCustomTabBatItems()
}

func layoutCustomTabBatItems() {
#warning("写法")
if itemsContainer == nil {
itemsContainer = UIView.init(frame: CGRect.init(x: 0, y: layout.miniPlayViewHeight, width: screenWidth, height: layout.tabBarHeight))
self.addSubview(itemsContainer!)

let itemW = screenWidth / CGFloat(tabBarItems.count)
for (index,barItem) in tabBarItems.enumerated() {
barItem.frame = CGRect.init(x: itemW*CGFloat(index), y: 0, width: itemW, height: layout.tabBarHeight)
itemsContainer!.addSubview(barItem)
}

}
}
}
52 changes: 51 additions & 1 deletion BottomBar/RootTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ class RootTabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

let tabBar = RootTabBar()

let musicTabBatItem = RootTabBarItem()
musicTabBatItem.icon = "tabbar_icon_account"
musicTabBatItem.title = "音乐"

let videoTabBatItem = RootTabBarItem()
videoTabBatItem.icon = "tabbar_icon_account"
videoTabBatItem.title = "视频"

let BBSTabBatItem = RootTabBarItem()
BBSTabBatItem.icon = "tabbar_icon_account"
BBSTabBatItem.title = "扑通"

let accountTabBatItem = RootTabBarItem()
accountTabBatItem.icon = "tabbar_icon_account"
accountTabBatItem.title = "我的"

tabBar.tabBarItems = [musicTabBatItem, videoTabBatItem, BBSTabBatItem, accountTabBatItem]

self.setValue(tabBar, forKey: "tabBar")

let musicVC = MusicViewController()
let image = UIImage.init(named: "tabbar_icon_account")
musicVC.tabBarItem = UITabBarItem.init(title: "音乐", image: image, tag: 0)
Expand All @@ -46,9 +69,36 @@ class RootTabBarController: UITabBarController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

// if (self.botttomBarStyle == BPBottomBarStyleNone) {
// tabBarStartingY = SCREEN_HEIGHT;
// playBarHeight = PLAY_BAR_HEIGHT;
// } else if (self.botttomBarStyle == BPBottomBarStylePlayBarOnly) {
// tabBarStartingY = SCREEN_HEIGHT - PLAY_BAR_HEIGHT - SAFE_BOTTOM_MARGIN;
// // 因buzz栏目有添加自定义的icon(此icon会超过UITabBarButton顶部4个像素),所以在只展示playbar时候要处理这个情况
// playBarHeight = PLAY_BAR_HEIGHT + SAFE_BOTTOM_MARGIN + 4;
// } else if (self.botttomBarStyle == BPBottomBarStyleAll) {
// tabBarStartingY = SCREEN_HEIGHT - TAB_BAR_HEIGHT - PLAY_BAR_HEIGHT - SAFE_BOTTOM_MARGIN;
// playBarHeight = PLAY_BAR_HEIGHT;
// }
//
// [self.tabBar setFrame: CGRectMake(0, tabBarStartingY, SCREEN_WIDTH, PLAY_BAR_HEIGHT + TAB_BAR_HEIGHT + SAFE_BOTTOM_MARGIN)];
// self.playBar.frame = CGRectMake(0, 0, SCREEN_WIDTH, playBarHeight);

var tabBarY: CGFloat

switch bottomBarStyle {
case .none:
tabBarY = screenHeight
case .all:
tabBarY = screenHeight - layout.tabBarHeight - layout.miniPlayViewHeight - safeAreaBottomHeight
case .miniPlayViewOnly:
tabBarY = screenHeight - layout.tabBarHeight
}

self.tabBar.frame = CGRect.init(x: 0, y: tabBarY, width: screenWidth, height: layout.tabBarHeight + layout.miniPlayViewHeight + safeAreaBottomHeight)
}

func updateBottomStyle(_ style:BottomBarStyle) {
func updateBottomStyle(_ style: BottomBarStyle) {
self.view.layoutIfNeeded()
bottomBarStyle = style
self.view.setNeedsLayout()
Expand Down
76 changes: 76 additions & 0 deletions BottomBar/RootTabBarItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//
// RootTabBarItem.swift
// BottomBar
//
// Created by 全达晖 on 2020/9/3.
// Copyright © 2020 全达晖. All rights reserved.
//

import UIKit

class RootTabBarItem: UIView {
// 对外暴露
var icon: String! {
didSet{
swappableImageView.image = UIImage.init(named: icon)
}
}
var title: String! {
didSet {
titleLabel.text = title
}
}
var titleColor: UIColor! {
didSet {
titleLabel.textColor = titleColor
}
}

// 私有属性
fileprivate
var swappableImageView: UIImageView!
var titleLabel: UILabel!

override init(frame: CGRect) {
super.init(frame: frame)
swappableImageView = UIImageView()
self.addSubview(swappableImageView)

titleLabel = UILabel()
titleLabel.textAlignment = .center
titleLabel.font = .systemFont(ofSize: 10)
titleLabel.numberOfLines = 0;
titleLabel.textColor = .systemGray
self.addSubview(titleLabel)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()

// 图片和文字 模式
if icon.count > 0 && title.count > 0 {
let imgW: CGFloat = 22
let imgH: CGFloat = imgW
let imgX: CGFloat = (self.frame.width - imgW) / 2
let imgY: CGFloat = 0
swappableImageView.frame = CGRect.init(x: imgX, y: imgY, width: imgW, height: imgH)

let titleX: CGFloat = 0
let titleY: CGFloat = swappableImageView.frame.maxY + 5
let titleW: CGFloat = self.frame.width;
let titleH: CGFloat = 18
titleLabel.frame = CGRect.init(x: titleX, y: titleY, width: titleW, height: titleH)
}
// 只有图片 模式 主题
else {
swappableImageView.bounds = self.bounds
}
}



}
20 changes: 12 additions & 8 deletions BottomBar/UIConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@

import Foundation
import UIKit
import AdSupport


#warning("todo 机型适配所用的宏定义 swift 怎么处理优雅")
struct UIConstants {
struct layout {
static let tabBarHeight: CGFloat = 50
static let miniPlayViewHeight: CGFloat = 44

}
struct layout {
static let tabBarHeight: CGFloat = 50
static let miniPlayViewHeight: CGFloat = 44
}

// 设备参数
let screenFrame: CGRect = UIScreen.main.bounds
let screenWidth = screenFrame.size.width
let screenHeight = screenFrame.size.height
let safeAreaTopHeight: CGFloat = (screenHeight >= 812.0 && UIDevice.current.model == "iPhone" ? 88 : 64)
let safeAreaBottomHeight: CGFloat = (screenHeight >= 812.0 && UIDevice.current.model == "iPhone" ? 30 : 0)

0 comments on commit 764a0f2

Please sign in to comment.