stack fixing. work in progress

This commit is contained in:
Pfeil, Scott Robert 2019-06-11 09:41:44 -04:00
parent 74fddc40ad
commit 36d223e0de
8 changed files with 136 additions and 124 deletions

View File

@ -39,7 +39,6 @@
D296E1412295EBBA0051EBE7 /* MoleculeDelegateProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = D296E1402295EBBA0051EBE7 /* MoleculeDelegateProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
D296E143229729C30051EBE7 /* MoleculeMappingObject+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D296E142229729C30051EBE7 /* MoleculeMappingObject+Extension.swift */; };
D296E14722A5984C0051EBE7 /* MVMCoreUIViewConstrainingProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = D296E14622A597490051EBE7 /* MVMCoreUIViewConstrainingProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
D296E15A22A7FBCC0051EBE7 /* HorizontalStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D296E15922A7FBCC0051EBE7 /* HorizontalStackView.swift */; };
D29770C821F7C4AE00B2F0D0 /* TopLabelsView.m in Sources */ = {isa = PBXBuildFile; fileRef = D29770C621F7C4AE00B2F0D0 /* TopLabelsView.m */; };
D29770C921F7C4AE00B2F0D0 /* TopLabelsView.h in Headers */ = {isa = PBXBuildFile; fileRef = D29770C721F7C4AE00B2F0D0 /* TopLabelsView.h */; settings = {ATTRIBUTES = (Public, ); }; };
D29770F221F7C6D600B2F0D0 /* TopLabelsAndBottomButtonsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D29770EE21F7C6D600B2F0D0 /* TopLabelsAndBottomButtonsTableViewController.m */; };
@ -205,7 +204,6 @@
D296E1402295EBBA0051EBE7 /* MoleculeDelegateProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MoleculeDelegateProtocol.h; sourceTree = "<group>"; };
D296E142229729C30051EBE7 /* MoleculeMappingObject+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MoleculeMappingObject+Extension.swift"; sourceTree = "<group>"; };
D296E14622A597490051EBE7 /* MVMCoreUIViewConstrainingProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MVMCoreUIViewConstrainingProtocol.h; sourceTree = "<group>"; };
D296E15922A7FBCC0051EBE7 /* HorizontalStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HorizontalStackView.swift; path = /Users/scott/Documents/Source/mvmrc_ios/mvm_core_ui/MVMCoreUI/Molecules/HorizontalStackView.swift; sourceTree = "<absolute>"; };
D29770C621F7C4AE00B2F0D0 /* TopLabelsView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TopLabelsView.m; sourceTree = "<group>"; };
D29770C721F7C4AE00B2F0D0 /* TopLabelsView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TopLabelsView.h; sourceTree = "<group>"; };
D29770EE21F7C6D600B2F0D0 /* TopLabelsAndBottomButtonsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TopLabelsAndBottomButtonsTableViewController.m; sourceTree = "<group>"; };
@ -465,7 +463,6 @@
D2A5145E2211DDC100345BFB /* MoleculeStackView.swift */,
D274CA322236A78900B01B62 /* StandardFooterView.swift */,
D2E1FADC2268B25E00AEFD8C /* MoleculeTableViewCell.swift */,
D296E15922A7FBCC0051EBE7 /* HorizontalStackView.swift */,
);
path = Molecules;
sourceTree = "<group>";
@ -916,7 +913,6 @@
D29DF27621E79E81003B2FB9 /* MVMCoreUILoggingHandler.m in Sources */,
D29DF24D21E6A177003B2FB9 /* MFTextField.m in Sources */,
D29DF2A221E7AF4E003B2FB9 /* MVMCoreUIUtility.m in Sources */,
D296E15A22A7FBCC0051EBE7 /* HorizontalStackView.swift in Sources */,
D29DF12B21E6851E003B2FB9 /* MVMCoreUITopAlertExpandableView.m in Sources */,
D29DF25421E6A177003B2FB9 /* MFMdnTextField.m in Sources */,
D282AABA224131D100C46919 /* MFTransparentGIFView.swift in Sources */,

View File

@ -308,10 +308,6 @@ import MVMCore
}
public func setAsMolecule() {
setContentHuggingPriority(.required, for: .vertical)
setContentHuggingPriority(.required, for: .horizontal)
//setContentCompressionResistancePriority(.required, for: .horizontal)
//setContentCompressionResistancePriority(.required, for: .vertical)
}
public func needsToBeConstrained() -> Bool {

View File

@ -25,6 +25,12 @@
@property (nullable, strong, nonatomic) NSLayoutConstraint *alignCenterTopPin;
@property (nullable, strong, nonatomic) NSLayoutConstraint *alignCenterBottomPin;
@property (nullable, strong, nonatomic) NSLayoutConstraint *leftPinLow;
@property (nullable, strong, nonatomic) NSLayoutConstraint *rightPinLow;
@property (nullable, strong, nonatomic) NSLayoutConstraint *topPinLow;
@property (nullable, strong, nonatomic) NSLayoutConstraint *bottomPinLow;
// In updateView, will set horizontal padding to default if set to YES.
@property (nonatomic) BOOL updateViewHorizontalDefaults;

View File

@ -61,6 +61,22 @@
self.alignCenterVerticalPin = [view.centerYAnchor constraintEqualToAnchor:view.superview.centerYAnchor];
self.alignCenterTopPin = [view.topAnchor constraintGreaterThanOrEqualToAnchor:view.superview.topAnchor];
self.alignCenterBottomPin = [view.superview.bottomAnchor constraintGreaterThanOrEqualToAnchor:view.bottomAnchor];
self.leftPinLow = [view.leftAnchor constraintEqualToAnchor:view.superview.leftAnchor];
self.leftPinLow.priority = 200;
self.leftPinLow.active = YES;
self.topPinLow = [view.topAnchor constraintEqualToAnchor:view.superview.topAnchor];
self.topPinLow.priority = 200;
self.topPinLow.active = YES;
self.rightPinLow = [view.superview.rightAnchor constraintEqualToAnchor:view.superview.rightAnchor];
self.rightPinLow.priority = 200;
self.rightPinLow.active = YES;
self.bottomPinLow = [view.superview.bottomAnchor constraintEqualToAnchor:view.superview.bottomAnchor];
self.bottomPinLow.priority = 200;
self.bottomPinLow.active = YES;
}
- (void)pinToSuperView {
@ -81,21 +97,25 @@
- (void)setLeftPinConstant:(CGFloat)constant {
self.leftPin.constant = constant;
self.alignCenterLeftPin.constant = constant;
self.leftPinLow.constant = constant;
}
- (void)setRightPinConstant:(CGFloat)constant {
self.rightPin.constant = constant;
self.alignCenterRightPin.constant = constant;
self.rightPinLow.constant = constant;
}
- (void)setTopPinConstant:(CGFloat)constant {
self.topPin.constant = constant;
self.alignCenterTopPin.constant = constant;
self.topPinLow.constant = constant;
}
- (void)setBottomPinConstant:(CGFloat)constant {
self.bottomPin.constant = constant;
self.alignCenterBottomPin.constant = constant;
self.bottomPinLow.constant = constant;
}
- (void)show {

View File

@ -55,7 +55,7 @@ extern NSString * _Nonnull const ConstraintWidth;
+ (nullable NSDictionary *)constraintPinView:(nonnull UIView*)view heightConstraint:(BOOL)pinHeight heightConstant:(CGFloat)heightConstant widthConstraint:(BOOL)pinWidth widthConstant:(CGFloat)widthConstant;
// pins 2 views in same hierarchy
+(nullable NSLayoutConstraint *)constraintPinFirstView :(nonnull UIView*)firstView toSecondView :(nonnull UIView*)secondView withConstant :(CGFloat)constant directionVertical :(BOOL)directionVertical;
+(nonnull NSLayoutConstraint *)constraintPinFirstView :(nonnull UIView*)firstView toSecondView :(nonnull UIView*)secondView withConstant :(CGFloat)constant directionVertical :(BOOL)directionVertical;
+(nullable NSDictionary *)constraintAlignView :(nonnull UIView *)firstView toSecondView :(nonnull UIView *)secondView alignX :(BOOL)alignX alignY :(BOOL)alignY;

View File

@ -1,75 +0,0 @@
//
// HorizontalStackView.swift
//
//
// Created by Scott Pfeil on 6/5/19.
//
import UIKit
@objcMembers public class HorizontalStackView: ViewConstrainingView {
let stackView = UIStackView(frame: .zero)
public override func updateView(_ size: CGFloat) {
super.updateView(size)
for view in stackView.arrangedSubviews {
stackView.removeArrangedSubview(view)
if let molecule = view as? MVMCoreViewProtocol {
molecule.updateView(size)
}
stackView.addArrangedSubview(view)
}
}
override public func setupView() {
super.setupView()
guard stackView.superview == nil else {
return
}
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.distribution = .fill
addConstrainedView(stackView)
}
public func setAxisWithJSON(_ json: [AnyHashable: Any]?) {
switch json?.optionalStringForKey("axis") {
case "horizontal":
stackView.axis = .horizontal
default:
stackView.axis = .vertical
}
}
override public func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable : Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
MVMCoreUIStackableViewController.remove(stackView.subviews)
// Set the horizontal alignment for the stack in the containing view. Typically fill, but occasionally others for horizontal stacks.
//align(ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("alignment"), defaultAlignment: UIStackView.Alignment.fill))
// Sets the stack attributes
setAxisWithJSON(json)
stackView.spacing = json?.optionalCGFloatForKey("spacing") ?? 16
// The alignment of the items perpendicular to the axis.
stackView.alignment = ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("itemAlignment"), defaultAlignment: UIStackView.Alignment.fill)
guard let molecules = json?.optionalArrayForKey("molecules") else {
return
}
for case let map as [AnyHashable: Any] in molecules {
if let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: map, delegateObject: delegateObject, constrainIfNeeded: true) {
if let moleculeCast = molecule as? MVMCoreUIViewConstrainingProtocol {
// Set the horizontal alignement of the molecule.
//moleculeCast.align!(ViewConstrainingView.getAlignmentFor(map.optionalStringForKey("alignment"), defaultAlignment: .fill))
// Currently no vertical margins.
moleculeCast.shouldSetVerticalMargins?(false)
moleculeCast.shouldSetHorizontalMargins?(stackView.axis == .vertical)
}
stackView.addArrangedSubview(molecule)
}
}
}
}

View File

@ -8,48 +8,49 @@
import UIKit
public class StackItem {
var view: UIView
var spacing: CGFloat?
var percentage: Int?
var verticalAlignment: UIStackView.Alignment?
var horizontalAlignment: UIStackView.Alignment?
init(with view: UIView) {
self.view = view
}
init(with view: UIView, json: [AnyHashable: Any]) {
self.view = view
spacing = json.optionalCGFloatForKey("spacing")
percentage = json["percent"] as? Int
if let alignment = json.optionalStringForKey("verticalAlignment") {
verticalAlignment = ViewConstrainingView.getAlignmentFor(alignment, defaultAlignment: .fill)
}
if let alignment = json.optionalStringForKey("horizontalAlignment") {
horizontalAlignment = ViewConstrainingView.getAlignmentFor(alignment, defaultAlignment: .fill)
}
}
}
public class MoleculeStackView: ViewConstrainingView {
var spacingBlock: ((Any) -> UIEdgeInsets)?
var moleculesArray: [UIView]?
var useMargins: Bool = false
var alignment: UIStackView.Alignment = .fill
var contentView: UIView = MVMCoreUICommonViewsUtility.commonView()
private var spacingBetweenItems: [NSLayoutConstraint]?
var contentView: UIView = MVMCoreUICommonViewsUtility.commonView()
var items: [StackItem] = []
private var spacingConstraints: [NSLayoutConstraint] = []
/// For setting the direction of the stack
var axis: NSLayoutConstraint.Axis = .vertical {
didSet {
if axis != oldValue {
MVMCoreDispatchUtility.performBlock(onMainThread: {
// remove constraints
if self.axis == .vertical {
// layout vertical
} else {
// layout horizontal
}
})
restack()
}
}
}
/// For setting the alignment perpendicular to the direction of the stack. Default fill for vertical and center for horizontal. Can be overriden at the item level.
var itemAlignment: UIStackView.Alignment = .fill {
didSet {
if itemAlignment != oldValue {
MVMCoreDispatchUtility.performBlock(onMainThread: {
// remove constraints
if self.axis == .vertical {
// layout vertical
} else {
// layout horizontal
}
})
}
}
}
var spacing: Float = 16 {
var spacing: CGFloat = 16 {
didSet {
if spacing != oldValue {
MVMCoreDispatchUtility.performBlock(onMainThread: {
@ -69,10 +70,11 @@ public class MoleculeStackView: ViewConstrainingView {
}
public func pinView(_ view: UIView, toView: UIView, attribute: NSLayoutConstraint.Attribute, relation: NSLayoutConstraint.Relation, priority: UILayoutPriority, constant: CGFloat) {
public func pinView(_ view: UIView, toView: UIView, attribute: NSLayoutConstraint.Attribute, relation: NSLayoutConstraint.Relation, priority: UILayoutPriority, constant: CGFloat) -> NSLayoutConstraint {
let constraint = NSLayoutConstraint(item: view, attribute: attribute, relatedBy: relation, toItem: toView, attribute: attribute, multiplier: 1.0, constant: constant)
constraint.priority = priority
constraint.isActive = true
return constraint
}
// MARK: - Inits
@ -103,8 +105,8 @@ public class MoleculeStackView: ViewConstrainingView {
translatesAutoresizingMaskIntoConstraints = false
backgroundColor = .clear
addConstrainedView(contentView)
//self.setContentHuggingPriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
//self.setContentCompressionResistancePriority(UILayoutPriority.required, for: NSLayoutConstraint.Axis.vertical)
contentView.setContentHuggingPriority(.defaultHigh, for: .vertical)
contentView.setContentHuggingPriority(.defaultHigh, for: .horizontal)
}
public override func updateView(_ size: CGFloat) {
@ -119,7 +121,7 @@ public class MoleculeStackView: ViewConstrainingView {
// MARK: - MVMCoreUIMoleculeViewProtocol
open override func setWithJSON(_ json: [AnyHashable: Any]?, delegateObject: MVMCoreUIDelegateObject?, additionalData: [AnyHashable: Any]?) {
super.setWithJSON(json, delegateObject: delegateObject, additionalData: additionalData)
MVMCoreUIStackableViewController.remove(contentView.subviews)
clear()
guard let molecules = json?.arrayForKey(KeyMolecules) as? [[String: Any]] else {
return
@ -127,7 +129,7 @@ public class MoleculeStackView: ViewConstrainingView {
// Sets the stack attributes
setAxisWithJSON(json)
spacing = json?["spacing"] as? Float ?? 16
spacing = json?.optionalCGFloatForKey("spacing") ?? 16
// Set the alignment for the stack in the containing view. The json driven value is for the axis direction alignment.
if axis == .vertical {
@ -139,16 +141,17 @@ public class MoleculeStackView: ViewConstrainingView {
}
// Create the molecules and set the json.
var previousObject = contentView
for (index, map) in molecules.enumerated() {
if let moleculeJSON = map.optionalDictionaryForKey(KeyMolecule), let molecule = MVMCoreUIMoleculeMappingObject.shared()?.createMolecule(forJSON: moleculeJSON, delegateObject: delegateObject, constrainIfNeeded: true) {
contentView.addSubview(molecule)
addStackItem(StackItem(with: molecule, json: map), lastItem: index == molecules.count - 1)
/*contentView.addSubview(molecule)
molecule.translatesAutoresizingMaskIntoConstraints = false
let spacing = CGFloat(map["spacing"] as? Float ?? self.spacing)
let percent = map["percent"] as? Int
let verticalAlignment = ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("verticalAlignment"), defaultAlignment: (percent == nil && axis == .vertical ? UIStackView.Alignment.fill : UIStackView.Alignment.leading))
let horizontalAlignment = ViewConstrainingView.getAlignmentFor(json?.optionalStringForKey("horizontalAlignment"), defaultAlignment: (axis == .vertical || percent == nil ? UIStackView.Alignment.fill : UIStackView.Alignment.leading))
let verticalAlignment = ViewConstrainingView.getAlignmentFor(map.optionalStringForKey("verticalAlignment"), defaultAlignment: (percent == nil && axis == .vertical ? UIStackView.Alignment.fill : UIStackView.Alignment.leading))
let horizontalAlignment = ViewConstrainingView.getAlignmentFor(map.optionalStringForKey("horizontalAlignment"), defaultAlignment: (axis == .vertical || percent == nil ? UIStackView.Alignment.fill : UIStackView.Alignment.leading))
if let molecule = molecule as? MVMCoreUIViewConstrainingProtocol {
molecule.alignHorizontal?(horizontalAlignment)
@ -184,6 +187,73 @@ public class MoleculeStackView: ViewConstrainingView {
pinView(contentView, toView: previousObject, attribute: .bottom, relation: .equal, priority: .required, constant: 0)
} else {
pinView(contentView, toView: previousObject, attribute: .right, relation: .equal, priority: .required, constant: 0)
}*/
}
}
}
// MARK: - Convenience Functions
func clear() {
MVMCoreUIStackableViewController.remove(contentView.subviews)
spacingConstraints = []
}
func restack() {
clear()
let stackItems = items
items.removeAll()
for (index, item) in stackItems.enumerated() {
addStackItem(item, lastItem: index == stackItems.count - 1)
}
}
/// Adds the view to the stack.
func addView(_ view: UIView, lastItem: Bool) {
addStackItem(StackItem(with: view), lastItem: lastItem)
}
/// Adds the stack item to the stack.
func addStackItem(_ stackItem: StackItem, lastItem: Bool) {
let view = stackItem.view
contentView.addSubview(view)
view.translatesAutoresizingMaskIntoConstraints = false
let spacing = stackItem.spacing ?? self.spacing
if let view = view as? MVMCoreUIViewConstrainingProtocol {
let verticalAlignment = stackItem.verticalAlignment ?? (stackItem.percentage == nil && axis == .vertical ? UIStackView.Alignment.fill : UIStackView.Alignment.leading)
let horizontalAlignment = stackItem.horizontalAlignment ?? view.alignment?() ?? (axis == .vertical || stackItem.percentage == nil ? UIStackView.Alignment.fill : UIStackView.Alignment.leading)
view.alignHorizontal?(horizontalAlignment)
view.alignVertical?(verticalAlignment)
}
if axis == .vertical {
if items.count == 0 {
spacingConstraints.append(pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: spacing))
} else if let previousView = items.last?.view {
spacingConstraints.append(NSLayoutConstraint(pinFirstView: previousView, toSecondView: view, withConstant: spacing, directionVertical: true))
}
pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: 0)
pinView(contentView, toView: view, attribute: .trailing, relation: .equal, priority: .required, constant: 0)
if let percent = stackItem.percentage {
view.heightAnchor.constraint(equalTo: contentView.heightAnchor, multiplier: CGFloat(percent)/100.0).isActive = true
}
if lastItem {
pinView(contentView, toView: view, attribute: .bottom, relation: .equal, priority: .required, constant: 0)
}
} else {
if items.count == 0 {
spacingConstraints.append(pinView(view, toView: contentView, attribute: .leading, relation: .equal, priority: .required, constant: spacing))
} else if let previousView = items.last?.view {
spacingConstraints.append(NSLayoutConstraint(pinFirstView: previousView, toSecondView: view, withConstant: spacing, directionVertical: false))
}
pinView(view, toView: contentView, attribute: .top, relation: .equal, priority: .required, constant: 0)
pinView(contentView, toView: view, attribute: .bottom, relation: .equal, priority: .required, constant: 0)
if let percent = stackItem.percentage {
view.widthAnchor.constraint(equalTo: contentView.widthAnchor, multiplier: CGFloat(percent)/100.0).isActive = true
}
}
if lastItem {
pinView(contentView, toView: view, attribute: .right, relation: .equal, priority: .required, constant: 0)
}
items.append(stackItem)
}
}

View File

@ -38,8 +38,7 @@
@"checkbox": MVMCoreUICheckBox.class,
@"listItem": MoleculeTableViewCell.class,
@"switchLineItem": SwitchLineItem.class,
@"switch": Switch.class,
@"testStack": HorizontalStackView.class
@"switch": Switch.class
} mutableCopy];
});
return mapping;