This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from hainayanda/shortcuts
Added Shortcuts for more cleaner code
- Loading branch information
Showing
40 changed files
with
2,006 additions
and
1,230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
Draftsman/Classes/Utilities/CustomView/ScrollableStackView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by Nayanda Haberty on 9/1/23. | ||
// | ||
|
||
import Foundation | ||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
public class ScrollableStackView: UIScrollView, Planned { | ||
let alignment: UIStackView.Alignment | ||
let spacing: CGFloat | ||
public var axis: NSLayoutConstraint.Axis { | ||
didSet { | ||
stackView.axis = axis | ||
} | ||
} | ||
|
||
lazy var stackView: UIStackView = UIStackView( | ||
axis: axis, | ||
distribution: .equalSpacing, | ||
alignment: alignment, | ||
spacing: spacing | ||
) | ||
|
||
public override var subviews: [UIView] { | ||
var allSubviews = super.subviews | ||
allSubviews.append(contentsOf: stackView.subviews) | ||
return allSubviews | ||
} | ||
|
||
@LayoutPlan | ||
public var viewPlan: ViewPlan { | ||
if axis == .vertical { | ||
stackView.drf | ||
.width.equal(with: .parent) | ||
.edges.equal(with: .parent) | ||
} else { | ||
stackView.drf | ||
.height.equal(with: .parent) | ||
.edges.equal(with: .parent) | ||
} | ||
} | ||
|
||
public override init(frame: CGRect) { | ||
self.alignment = .center | ||
self.spacing = .zero | ||
self.axis = .vertical | ||
super.init(frame: frame) | ||
} | ||
|
||
public init( | ||
frame: CGRect = .zero, | ||
axis: NSLayoutConstraint.Axis, | ||
margins: UIEdgeInsets? = nil, | ||
alignment: UIStackView.Alignment = .center, | ||
spacing: CGFloat = .zero) { | ||
self.axis = axis | ||
self.alignment = alignment | ||
self.spacing = spacing | ||
super.init(frame: frame) | ||
if let margins = margins { | ||
self.stackView.layoutMargins = margins | ||
self.stackView.isLayoutMarginsRelativeArrangement = true | ||
} | ||
applyPlan() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
self.axis = .vertical | ||
self.alignment = .center | ||
self.spacing = .zero | ||
super.init(coder: coder) | ||
applyPlan() | ||
} | ||
} | ||
|
||
extension ScrollableStackView: StackCompatible { | ||
|
||
public override var layoutMargins: UIEdgeInsets { | ||
get { | ||
stackView.layoutMargins | ||
} | ||
set { | ||
stackView.layoutMargins = newValue | ||
} | ||
} | ||
|
||
public var isLayoutMarginsRelativeArrangement: Bool { | ||
get { | ||
stackView.isLayoutMarginsRelativeArrangement | ||
} | ||
set { | ||
stackView.isLayoutMarginsRelativeArrangement = newValue | ||
} | ||
} | ||
|
||
public var arrangedSubviews: [UIView] { | ||
stackView.arrangedSubviews | ||
} | ||
|
||
public func addArrangedSubview(_ view: UIView) { | ||
stackView.addArrangedSubview(view) | ||
} | ||
|
||
public func removeArrangedSubview(_ view: UIView) { | ||
stackView.removeArrangedSubview(view) | ||
} | ||
|
||
public func insertArrangedSubview(_ view: UIView, at stackIndex: Int) { | ||
stackView.insertArrangedSubview(view, at: stackIndex) | ||
} | ||
|
||
@available(iOS 11.0, *) | ||
public func setCustomSpacing(_ spacing: CGFloat, after arrangedSubview: UIView) { | ||
stackView.setCustomSpacing(spacing, after: arrangedSubview) | ||
} | ||
|
||
@available(iOS 11.0, *) | ||
public func customSpacing(after arrangedSubview: UIView) -> CGFloat { | ||
stackView.customSpacing(after: arrangedSubview) | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// SpacerView.swift | ||
// Draftsman | ||
// | ||
// Created by Nayanda Haberty on 11/1/23. | ||
// | ||
|
||
import Foundation | ||
#if canImport(UIKit) | ||
import UIKit | ||
|
||
public class SpacerView: UIView { | ||
|
||
let dimension: CGFloat? | ||
var spaceConstraint: NSLayoutConstraint? | ||
|
||
public init(_ dimension: CGFloat) { | ||
self.dimension = dimension | ||
super.init(frame: .zero) | ||
self.backgroundColor = .clear | ||
} | ||
|
||
public init() { | ||
self.dimension = nil | ||
super.init(frame: .zero) | ||
self.backgroundColor = .clear | ||
} | ||
|
||
func apply(axis: NSLayoutConstraint.Axis) { | ||
spaceConstraint?.isActive = false | ||
guard let dimension = self.dimension else { | ||
super.setContentCompressionResistancePriority(.defaultLow, for: axis) | ||
super.setContentHuggingPriority(.defaultLow, for: axis) | ||
super.setContentCompressionResistancePriority( | ||
defaultContentCompressionResistancePriority(for: axis.crossAxis), | ||
for: axis.crossAxis | ||
) | ||
super.setContentHuggingPriority( | ||
defaultContentHuggingPriority(for: axis.crossAxis), | ||
for: axis.crossAxis | ||
) | ||
return | ||
} | ||
switch axis { | ||
case .horizontal: | ||
spaceConstraint = widthAnchor.constraint(greaterThanOrEqualToConstant: dimension) | ||
spaceConstraint?.isActive = true | ||
case .vertical: | ||
spaceConstraint = heightAnchor.constraint(greaterThanOrEqualToConstant: dimension) | ||
spaceConstraint?.isActive = true | ||
@unknown default: | ||
break | ||
} | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
self.dimension = nil | ||
super.init(coder: coder) | ||
self.backgroundColor = .clear | ||
} | ||
|
||
private var userContentHuggingPriority: [NSLayoutConstraint.Axis: UILayoutPriority] = [:] | ||
public override func setContentHuggingPriority(_ priority: UILayoutPriority, for axis: NSLayoutConstraint.Axis) { | ||
super.setContentHuggingPriority(priority, for: axis) | ||
userContentHuggingPriority[axis] = priority | ||
} | ||
|
||
private var userContentCompressionResistancePriority: [NSLayoutConstraint.Axis: UILayoutPriority] = [:] | ||
public override func setContentCompressionResistancePriority(_ priority: UILayoutPriority, for axis: NSLayoutConstraint.Axis) { | ||
super.setContentCompressionResistancePriority(priority, for: axis) | ||
userContentCompressionResistancePriority[axis] = priority | ||
} | ||
|
||
private func defaultContentHuggingPriority(for axis: NSLayoutConstraint.Axis) -> UILayoutPriority { | ||
userContentHuggingPriority[axis.crossAxis] ?? UILayoutPriority(250) | ||
} | ||
|
||
private func defaultContentCompressionResistancePriority(for axis: NSLayoutConstraint.Axis) -> UILayoutPriority { | ||
userContentCompressionResistancePriority[axis.crossAxis] ?? UILayoutPriority(750) | ||
} | ||
|
||
} | ||
|
||
extension NSLayoutConstraint.Axis { | ||
var crossAxis: NSLayoutConstraint.Axis { | ||
switch self { | ||
case .horizontal: | ||
return .vertical | ||
case .vertical: | ||
return .horizontal | ||
@unknown default: | ||
return self | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.