Skip to content

Commit

Permalink
Support padding and margin
Browse files Browse the repository at this point in the history
  • Loading branch information
Nonchalant committed Jul 22, 2020
1 parent 7c7d2e1 commit 1ac4111
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 48 deletions.
35 changes: 19 additions & 16 deletions Sources/SwipeMenuView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public struct SwipeMenuViewOptions {
/// ItemView width. Defaults to `100.0`.
public var width: CGFloat = 100.0

/// ItemView side margin. Defaults to `5.0`.
public var margin: CGFloat = 5.0
/// ItemView side padding. Defaults to `5.0`.
public var padding: CGFloat = 5.0

/// ItemView font. Defaults to `14 pt as bold SystemFont`.
public var font: UIFont = UIFont.boldSystemFont(ofSize: 14)
Expand All @@ -38,12 +38,12 @@ public struct SwipeMenuViewOptions {
}

public struct AdditionView {

public struct Underline {
/// Underline height if addition style select `.underline`. Defaults to `2.0`.
public var height: CGFloat = 2.0
}

public struct Circle {
/// Circle cornerRadius if addition style select `.circle`. Defaults to `nil`.
/// `AdditionView.height / 2` in the case of nil.
Expand All @@ -56,25 +56,25 @@ public struct SwipeMenuViewOptions {

/// AdditionView paddings. Defaults to `.zero`.
public var padding: UIEdgeInsets = .zero

/// AdditionView backgroundColor. Defaults to `.black`.
public var backgroundColor: UIColor = .black

/// AdditionView animating duration. Defaults to `0.3`.
public var animationDuration: Double = 0.3

/// Underline style options.
public var underline = Underline()

/// Circle style options.
public var circle = Circle()
}

/// TabView height. Defaults to `44.0`.
public var height: CGFloat = 44.0

/// TabView side margin. Defaults to `0.0`.
public var margin: CGFloat = 0.0
/// TabView side padding. Defaults to `zero`.
public var padding: UIEdgeInsets = .zero

/// TabView background color. Defaults to `.clear`.
public var backgroundColor: UIColor = .clear
Expand Down Expand Up @@ -122,6 +122,9 @@ public struct SwipeMenuViewOptions {

/// ContentScrollView preloading previous/next page count. Defaults to `1`.
public var preloadingPageCount: Int = 1

/// ContentScrollView margin. Defaults to `zero`.
public var margin: UIEdgeInsets = .zero
}

/// TabView and ContentScrollView Enable safeAreaLayout. Defaults to `true`.
Expand Down Expand Up @@ -319,10 +322,10 @@ open class SwipeMenuView: UIView {

backgroundColor = .clear

contentScrollView = ContentScrollView(frame: CGRect(x: 0, y: options.tabView.height, width: frame.width, height: frame.height - options.tabView.height), default: defaultIndex, options: options.contentScrollView)
contentScrollView = ContentScrollView(frame: .zero, default: defaultIndex, options: options.contentScrollView)
contentScrollView?.clipsToBounds = options.contentScrollView.clipsToBounds

tabView = TabView(frame: CGRect(x: 0, y: 0, width: frame.width, height: options.tabView.height), options: options.tabView)
tabView = TabView(frame: .zero, options: options.tabView)
tabView?.clipsToBounds = options.tabView.clipsToBounds

tabView?.update(defaultIndex)
Expand All @@ -340,7 +343,7 @@ open class SwipeMenuView: UIView {
tabView.topAnchor.constraint(equalTo: self.topAnchor),
tabView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
tabView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
tabView.heightAnchor.constraint(equalToConstant: options.tabView.height)
tabView.heightAnchor.constraint(equalToConstant: options.tabView.height + options.tabView.padding.vertical)
])
}

Expand All @@ -349,10 +352,10 @@ open class SwipeMenuView: UIView {
contentScrollView.translatesAutoresizingMaskIntoConstraints = false

NSLayoutConstraint.activate([
contentScrollView.topAnchor.constraint(equalTo: self.topAnchor, constant: options.tabView.height),
contentScrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
contentScrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
contentScrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
contentScrollView.topAnchor.constraint(equalTo: self.topAnchor, constant: (options.tabView.height + options.tabView.padding.vertical) + options.contentScrollView.margin.top),
contentScrollView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.contentScrollView.margin.left),
contentScrollView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: options.contentScrollView.margin.right),
contentScrollView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: options.contentScrollView.margin.bottom)
])
}

Expand Down
80 changes: 48 additions & 32 deletions Sources/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ open class TabView: UIScrollView {
open override func safeAreaInsetsDidChange() {
super.safeAreaInsetsDidChange()

leftMarginConstraint.constant = options.margin + safeAreaInsets.left
leftMarginConstraint.constant = options.padding.left + safeAreaInsets.left
if options.style == .segmented {
widthConstraint.constant = options.margin * -2 - safeAreaInsets.left - safeAreaInsets.right
widthConstraint.constant = -options.padding.horizontal - safeAreaInsets.left - safeAreaInsets.right
}

layoutIfNeeded()
Expand All @@ -87,11 +87,11 @@ open class TabView: UIScrollView {
let contentWidth: CGFloat

if #available(iOS 11.0, *), options.isSafeAreaEnabled {
offset = target.center.x + options.margin + safeAreaInsets.left - self.frame.width / 2
contentWidth = containerView.frame.width + options.margin * 2 + safeAreaInsets.left + safeAreaInsets.right
offset = target.center.x + options.padding.left + safeAreaInsets.left - self.frame.width / 2
contentWidth = containerView.frame.width + options.padding.horizontal + safeAreaInsets.left + safeAreaInsets.right
} else {
offset = target.center.x + options.margin - self.frame.width / 2
contentWidth = containerView.frame.width + options.margin * 2
offset = target.center.x + options.padding.left - self.frame.width / 2
contentWidth = containerView.frame.width + options.padding.horizontal
}

if offset < 0 || self.frame.width > contentWidth {
Expand Down Expand Up @@ -182,19 +182,26 @@ open class TabView: UIScrollView {
case .flexible:
let containerWidth = options.itemView.width * CGFloat(itemCount)
if #available(iOS 11.0, *), options.isSafeAreaEnabled {
contentSize = CGSize(width: containerWidth + options.margin * 2 + safeAreaInsets.left + safeAreaInsets.right, height: options.height)
containerView.frame = CGRect(x: 0, y: options.margin + safeAreaInsets.left, width: containerWidth, height: containerHeight)
contentSize = CGSize(width: containerWidth + options.padding.horizontal + safeAreaInsets.left + safeAreaInsets.right,
height: options.height)
containerView.frame = CGRect(x: 0,
y: options.padding.top + options.padding.left + safeAreaInsets.left,
width: containerWidth,
height: containerHeight)
} else {
contentSize = CGSize(width: containerWidth + options.margin * 2, height: options.height)
containerView.frame = CGRect(x: 0, y: options.margin, width: containerWidth, height: containerHeight)
contentSize = CGSize(width: containerWidth + options.padding.horizontal, height: options.height)
containerView.frame = CGRect(x: 0, y: options.padding.top + options.padding.left, width: containerWidth, height: containerHeight)
}
case .segmented:
if #available(iOS 11.0, *), options.isSafeAreaEnabled {
contentSize = CGSize(width: frame.width, height: options.height)
containerView .frame = CGRect(x: 0, y: options.margin + safeAreaInsets.left, width: frame.width - options.margin * 2 - safeAreaInsets.left - safeAreaInsets.right, height: containerHeight)
containerView.frame = CGRect(x: 0,
y: (options.padding.top + options.padding.left) + safeAreaInsets.left,
width: frame.width - options.padding.horizontal - safeAreaInsets.left - safeAreaInsets.right,
height: containerHeight)
} else {
contentSize = CGSize(width: frame.width, height: options.height)
containerView .frame = CGRect(x: 0, y: options.margin, width: frame.width - options.margin * 2, height: containerHeight)
containerView.frame = CGRect(x: 0, y: (options.padding.top + options.padding.left), width: frame.width - options.padding.horizontal, height: containerHeight)
}
}

Expand Down Expand Up @@ -227,7 +234,7 @@ open class TabView: UIScrollView {
case .flexible:
if options.needsAdjustItemViewWidth {
var adjustCellSize = tabItemView.frame.size
adjustCellSize.width = tabItemView.titleLabel.sizeThatFits(containerView.frame.size).width + options.itemView.margin * 2
adjustCellSize.width = tabItemView.titleLabel.sizeThatFits(containerView.frame.size).width + options.itemView.padding * 2
tabItemView.frame.size = adjustCellSize

containerView.addArrangedSubview(tabItemView)
Expand All @@ -245,9 +252,11 @@ open class TabView: UIScrollView {
case .segmented:
let adjustCellSize: CGSize
if #available(iOS 11.0, *), options.isSafeAreaEnabled {
adjustCellSize = CGSize(width: (frame.width - options.margin * 2 - safeAreaInsets.left - safeAreaInsets.right) / CGFloat(itemCount), height: tabItemView.frame.size.height)
adjustCellSize = CGSize(width: (frame.width - options.padding.horizontal - safeAreaInsets.left - safeAreaInsets.right) / CGFloat(itemCount),
height: tabItemView.frame.size.height)
} else {
adjustCellSize = CGSize(width: (frame.width - options.margin * 2) / CGFloat(itemCount), height: tabItemView.frame.size.height)
adjustCellSize = CGSize(width: (frame.width - options.padding.horizontal) / CGFloat(itemCount),
height: tabItemView.frame.size.height)
}
tabItemView.frame.size = adjustCellSize

Expand All @@ -273,7 +282,7 @@ open class TabView: UIScrollView {

containerView.frame.size.width = containerWidth
containerView.translatesAutoresizingMaskIntoConstraints = false

let heightConstraint: NSLayoutConstraint
switch options.addition {
case .underline:
Expand All @@ -285,40 +294,40 @@ open class TabView: UIScrollView {
switch options.style {
case .flexible:
if #available(iOS 11.0, *), options.isSafeAreaEnabled {
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.margin + safeAreaInsets.left)
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.padding.left + safeAreaInsets.left)

NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: self.topAnchor),
containerView.topAnchor.constraint(equalTo: self.topAnchor, constant: options.padding.top),
leftMarginConstraint,
containerView.widthAnchor.constraint(equalToConstant: containerWidth),
heightConstraint
])
contentSize.width = containerWidth + options.margin * 2 + safeAreaInsets.left - safeAreaInsets.right
contentSize.width = containerWidth + options.padding.horizontal + safeAreaInsets.left - safeAreaInsets.right
} else {
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.margin)
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.padding.left)
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: self.topAnchor),
containerView.topAnchor.constraint(equalTo: self.topAnchor, constant: options.padding.top),
leftMarginConstraint,
containerView.widthAnchor.constraint(equalToConstant: containerWidth),
heightConstraint
])
contentSize.width = containerWidth + options.margin * 2
contentSize.width = containerWidth + options.padding.horizontal
}
case .segmented:
if #available(iOS 11.0, *), options.isSafeAreaEnabled {
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.margin + safeAreaInsets.left)
widthConstraint = containerView.widthAnchor.constraint(equalTo: self.widthAnchor, constant: options.margin * -2 - safeAreaInsets.left - safeAreaInsets.right)
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.padding.left + safeAreaInsets.left)
widthConstraint = containerView.widthAnchor.constraint(equalTo: self.widthAnchor, constant: -options.padding.horizontal - safeAreaInsets.left - safeAreaInsets.right)
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: self.topAnchor),
containerView.topAnchor.constraint(equalTo: self.topAnchor, constant: options.padding.top),
leftMarginConstraint,
widthConstraint,
heightConstraint
])
} else {
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.margin)
widthConstraint = containerView.widthAnchor.constraint(equalTo: self.widthAnchor, constant: options.margin * -2)
leftMarginConstraint = containerView.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: options.padding.left)
widthConstraint = containerView.widthAnchor.constraint(equalTo: self.widthAnchor, constant: -options.padding.horizontal)
NSLayoutConstraint.activate([
containerView.topAnchor.constraint(equalTo: self.topAnchor),
containerView.topAnchor.constraint(equalTo: self.topAnchor, constant: options.padding.top),
leftMarginConstraint,
widthConstraint,
heightConstraint
Expand Down Expand Up @@ -351,13 +360,20 @@ extension TabView {
switch options.addition {
case .underline:
let itemView = itemViews[currentIndex]
additionView = UIView(frame: CGRect(x: itemView.frame.origin.x + options.additionView.padding.left, y: itemView.frame.height - options.additionView.padding.vertical, width: itemView.frame.width - options.additionView.padding.horizontal, height: options.additionView.underline.height))
let itemHeight = options.height - options.additionView.underline.height - options.additionView.padding.bottom
additionView = UIView(frame: CGRect(x: itemView.frame.origin.x + options.additionView.padding.left,
y: itemHeight - options.additionView.padding.vertical,
width: itemView.frame.width - options.additionView.padding.horizontal,
height: options.additionView.underline.height))
additionView.backgroundColor = options.additionView.backgroundColor
containerView.addSubview(additionView)
case .circle:
let itemView = itemViews[currentIndex]
let height = itemView.bounds.height - options.additionView.padding.vertical
additionView = UIView(frame: CGRect(x: itemView.frame.origin.x + options.additionView.padding.left, y: 0, width: itemView.frame.width - options.additionView.padding.horizontal, height: height))
additionView = UIView(frame: CGRect(x: itemView.frame.origin.x + options.additionView.padding.left,
y: 0,
width: itemView.frame.width - options.additionView.padding.horizontal,
height: height))
additionView.layer.position.y = itemView.layer.position.y
additionView.layer.cornerRadius = options.additionView.circle.cornerRadius ?? additionView.frame.height / 2
additionView.backgroundColor = options.additionView.backgroundColor
Expand Down Expand Up @@ -389,9 +405,9 @@ extension TabView {
dataSource.numberOfItems(in: self) > 0 else { return }
let adjustCellWidth: CGFloat
if #available(iOS 11.0, *), options.isSafeAreaEnabled && safeAreaInsets != .zero {
adjustCellWidth = (frame.width - options.margin * 2 - safeAreaInsets.left - safeAreaInsets.right) / CGFloat(dataSource.numberOfItems(in: self)) - options.additionView.padding.horizontal
adjustCellWidth = (frame.width - options.padding.horizontal - safeAreaInsets.left - safeAreaInsets.right) / CGFloat(dataSource.numberOfItems(in: self)) - options.additionView.padding.horizontal
} else {
adjustCellWidth = (frame.width - options.margin * 2) / CGFloat(dataSource.numberOfItems(in: self)) - options.additionView.padding.horizontal
adjustCellWidth = (frame.width - options.padding.horizontal) / CGFloat(dataSource.numberOfItems(in: self)) - options.additionView.padding.horizontal
}

additionView.frame.origin.x = adjustCellWidth * CGFloat(index) - options.additionView.padding.left
Expand Down

0 comments on commit 1ac4111

Please sign in to comment.