Skip to content

Commit

Permalink
add layoutAlignment, offsetX for barStyle;
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Tong authored and Andy Tong committed Sep 11, 2017
1 parent 7268168 commit 95f1130
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 43 deletions.
32 changes: 22 additions & 10 deletions AHCategoryView/Classes/AHCategoryNavBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public class AHCategoryNavBar: UIView {

fileprivate lazy var buttons = [UIButton]()

/// The paddings stuck before/after a AHCategoryItem
fileprivate var itemPadding: CGFloat = 8.0

// each key is the index from categories or buttons
fileprivate lazy var badgeDict = [Int : BadgeInfo]()

Expand Down Expand Up @@ -230,9 +233,7 @@ fileprivate extension AHCategoryNavBar {
}
func setupScrollView(){
scrollView.showsHorizontalScrollIndicator = false
if !barStyle.isScrollabel {
scrollView.isScrollEnabled = false
}
scrollView.isScrollEnabled = barStyle.isScrollabel
addSubview(scrollView)
}

Expand Down Expand Up @@ -272,21 +273,32 @@ fileprivate extension AHCategoryNavBar {

if barStyle.isScrollabel {
// scrollabel, each label has its own width according to its text
width = textWidth
width = textWidth + itemPadding * 2
if i > 0 {
let previousBtn = buttons[i - 1]
x = previousBtn.frame.maxX + barStyle.interItemSpace
x = previousBtn.frame.maxX + barStyle.interItemSpace + barStyle.offsetX
}
// special adjustment for the default button
if i == barStyle.defaultCategoryIndex {
x = barStyle.interItemSpace * 0.5
}
}else{
// not scrollabel, then divide width equally for all labels
width = self.bounds.width / CGFloat(categories.count)

if i > 0 {
x = width * CGFloat(i)
if barStyle.layoutAlignment == .left {
width = textWidth + itemPadding * 2
if i > 0 {
let previousBtn = buttons[i - 1]
x = previousBtn.frame.maxX + barStyle.interItemSpace + barStyle.offsetX
}
// special adjustment for the default button
if i == barStyle.defaultCategoryIndex {
x = barStyle.interItemSpace * 0.5
}
}else {
width = self.bounds.width / CGFloat(categories.count)

if i > 0 {
x = width * CGFloat(i) + barStyle.offsetX
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions AHCategoryView/Classes/AHCategoryNavBarStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@

import UIKit

public enum AHCategoryNavBarAlignment {
case left
case center
}

public struct AHCategoryNavBarStyle {
public var height: CGFloat = 44.0

/// The x offset for the navBar in its superView
public var offsetX: CGFloat = 0.0

/// Laying out category items from left to right, or from center to both sides.
/// Note: This attribute only works with when isScrollabel=false.
public var layoutAlignment: AHCategoryNavBarAlignment = .left

/// if set to true, all items will have equal spacing.
public var isScrollabel = false
public var fontSize: CGFloat = 15.0
Expand Down
22 changes: 21 additions & 1 deletion Example/AHCategoryView/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="q1P-tN-qDe">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
Expand All @@ -22,9 +22,29 @@
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
<navigationItem key="navigationItem" id="i5a-3M-EQJ"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1076" y="138.98050974512745"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="Nhb-nz-86g">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="q1P-tN-qDe" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="gjJ-zB-8cg">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="rootViewController" id="PTU-4V-SCv"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="V2b-1r-aaW" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="244" y="-543.77811094452773"/>
</scene>
</scenes>
</document>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "[email protected]",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 48 additions & 32 deletions Example/AHCategoryView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,66 @@

import UIKit
import AHCategoryView

private let ScreenSize = UIScreen.main.bounds.size

class ViewController: UIViewController {
let categoryTitles = ["Featured", "Charts"]
var categoryView: AHCategoryView!
let categoryTitles = ["Featured", "Charts","Charts","Charts","Charts"]
fileprivate weak var categoryView: AHCategoryView!
var childVCs = [UIViewController]()

override func viewDidLoad() {
super.viewDidLoad()
var childVCs = [UIViewController]()
// the extra 1 is for the first meItem
for _ in 0..<(categoryTitles.count) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.random()
childVCs.append(vc)
}
let searchBtn = UIButton(type: .custom)
let searchImg = UIImage(named: "search-magnifier")
searchBtn.setImage(searchImg, for: .normal)
searchBtn.sizeToFit()
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchBtn)

var style = AHCategoryNavBarStyle()
style.isScrollabel = false
style.showIndicator = true
style.normalColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0)
style.selectedColor = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
style.showBgMaskView = false

// test custom maskView
// let customeMask = UIView()
// customeMask.backgroundColor = UIColor.yellow
// style.bgMaskView = customeMask
var meItem = AHCategoryItem()
meItem.normalImage = UIImage(named: "me-normal")
meItem.selectedImage = UIImage(named: "me-selected")


var featureItem = AHCategoryItem()
featureItem.title = "Feature"
var chartItem = AHCategoryItem()
chartItem.title = "Categories"
var radioItem = AHCategoryItem()
radioItem.title = "Radio"
var liveItem = AHCategoryItem()
liveItem.title = "Live"

style.showTransitionAnimation = true
let frame = CGRect(x: 0, y: 64, width: view.bounds.width, height: view.bounds.height - 64)

let items = [meItem, featureItem, chartItem]

var categoryItems = [AHCategoryItem]()
var meItem = AHCategoryItem()
meItem.normalImage = UIImage(named: "me-normal")
meItem.selectedImage = UIImage(named: "me-selected")
// categoryItems.append(meItem)
categoryTitles.forEach { (titel) in
var item = AHCategoryItem()
item.title = titel
categoryItems.append(item)

for _ in 0..<3 {
let vc = UIViewController()
vc.view.backgroundColor = UIColor.red
childVCs.append(vc)
}
categoryView = AHCategoryView(frame: frame, categories: categoryItems, childVCs: childVCs, parentVC: self, barStyle: style)

view.addSubview(categoryView)
let frame = CGRect(x: 0, y: 64.0, width: ScreenSize.width, height: ScreenSize.height - 64.0)
var style = AHCategoryNavBarStyle()
// style.offsetX = -16.0
style.isScrollabel = false
style.layoutAlignment = .center
style.isEmbedded = false
style.showSeparators = false
style.indicatorColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1.0)
style.normalColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0)
style.selectedColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1.0)
style.defaultCategoryIndex = 1
self.view.backgroundColor = UIColor.white

let categoryView = AHCategoryView(frame: frame, categories: items, childVCs: childVCs, parentVC: self, barStyle: style)
self.view.addSubview(categoryView)
self.categoryView = categoryView
categoryView.navBar.frame = CGRect(x: 0, y: 0, width: 359.0, height: 44.0)
self.navigationItem.titleView = categoryView.navBar
self.navigationController?.navigationBar.barTintColor = UIColor.white
}


Expand Down

0 comments on commit 95f1130

Please sign in to comment.