mirror of https://github.com/SnapKit/SnapKit
ViewConstraint offset methods
This commit is contained in:
parent
546c6e60ee
commit
6212ee9575
|
@ -70,12 +70,12 @@ class CompositeConstraint: Constraint, ConstraintDelegate {
|
|||
return self
|
||||
}
|
||||
|
||||
func insets(insets: Any) -> Constraint {
|
||||
func offset(offset: Any) -> Constraint {
|
||||
return self
|
||||
}
|
||||
|
||||
func offset(offset: Any) -> Constraint {
|
||||
return self
|
||||
func insets(insets: UIEdgeInsets) -> Constraint {
|
||||
return self;
|
||||
}
|
||||
|
||||
func multipliedBy(multiplier: Float) -> Constraint {
|
||||
|
|
|
@ -41,7 +41,7 @@ typealias Delegate = ConstraintDelegate?
|
|||
|
||||
func lessThanOrEqualTo(attr: Any) -> Constraint
|
||||
|
||||
func insets(insets: Any) -> Constraint
|
||||
func insets(insets: UIEdgeInsets) -> Constraint
|
||||
|
||||
func offset(offset: Any) -> Constraint
|
||||
|
||||
|
|
|
@ -17,15 +17,18 @@ class ViewConstraint: Constraint {
|
|||
var layoutPriority = 1000.0// UILayoutPriorityRequired gives error?!
|
||||
var layoutMultiplier = 1.0
|
||||
var layoutConstraint: NSLayoutConstraint?
|
||||
var layoutRelation = NSLayoutRelation.Equal
|
||||
var layoutConstant = 0.0
|
||||
var hasLayoutRelation = false;
|
||||
var layoutRelation: NSLayoutRelation?
|
||||
var layoutConstant: Float
|
||||
|
||||
weak var delegate: ConstraintDelegate?
|
||||
|
||||
init(view: View, firstViewAttribute: ViewAttribute) {
|
||||
self.view = view;
|
||||
self.firstViewAttribute = firstViewAttribute;
|
||||
|
||||
self.layoutPriority = 1000.0// UILayoutPriorityRequired gives error?!
|
||||
self.layoutMultiplier = 1.0
|
||||
self.layoutConstant = 0.0
|
||||
}
|
||||
|
||||
var left: Constraint { return addConstraint(.Left) }
|
||||
|
@ -43,20 +46,112 @@ class ViewConstraint: Constraint {
|
|||
var and: Constraint { return self }
|
||||
var with: Constraint { return self }
|
||||
|
||||
func addConstraint(NSLayoutAttribute) -> Constraint {
|
||||
func addConstraint(attr: NSLayoutAttribute) -> Constraint {
|
||||
if (self.layoutRelation) {
|
||||
//TODO use assert
|
||||
println("Attributes should be chained before defining the constraint relation")
|
||||
}
|
||||
|
||||
return self.delegate!.constraint(self, addConstraintWithLayoutAttribute:attr)
|
||||
}
|
||||
|
||||
func equality(relation: NSLayoutRelation, attr: Any) -> Constraint {
|
||||
layoutRelation = relation
|
||||
|
||||
switch attr {
|
||||
case let view as View:
|
||||
secondViewAttribute = ViewAttribute(view: firstViewAttribute.view, layoutAttribute: firstViewAttribute.layoutAttribute)
|
||||
case let offset as Float:
|
||||
layoutConstant = offset
|
||||
case let number as NSNumber:
|
||||
layoutConstant = number
|
||||
case let viewAttribute as ViewAttribute:
|
||||
secondViewAttribute = viewAttribute
|
||||
case let size as CGSize:
|
||||
sizeOffset(size)
|
||||
case let point as CGPoint:
|
||||
centerOffset(point)
|
||||
case let inset as UIEdgeInsets:
|
||||
insets(inset)
|
||||
default:
|
||||
println("unsupported value: \(attr)")
|
||||
}
|
||||
|
||||
// } else if (strcmp(value.objCType, @encode(CGPoint)) == 0) {
|
||||
// CGPoint point;
|
||||
// [value getValue:&point];
|
||||
// self.centerOffset = point;
|
||||
// } else if (strcmp(value.objCType, @encode(CGSize)) == 0) {
|
||||
// CGSize size;
|
||||
// [value getValue:&size];
|
||||
// self.sizeOffset = size;
|
||||
// } else if (strcmp(value.objCType, @encode(MASEdgeInsets)) == 0) {
|
||||
// MASEdgeInsets insets;
|
||||
// [value getValue:&insets];
|
||||
// self.insets = insets;
|
||||
// } else {
|
||||
// NSAssert(NO, @"attempting to set layout constant with unsupported value: %@", value);
|
||||
// }
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
private func sizeOffset(size: CGSize) {
|
||||
switch (firstViewAttribute.layoutAttribute) {
|
||||
case .Width:
|
||||
layoutConstant = Float(size.width);
|
||||
break;
|
||||
case .Height:
|
||||
layoutConstant = Float(size.height);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private func centerOffset(point: CGPoint) {
|
||||
switch (firstViewAttribute.layoutAttribute) {
|
||||
case .CenterX:
|
||||
layoutConstant = Float(point.x);
|
||||
break;
|
||||
case .CenterY:
|
||||
layoutConstant = Float(point.y);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
func insets(insets: UIEdgeInsets) -> Constraint {
|
||||
switch (firstViewAttribute.layoutAttribute) {
|
||||
case .Left:
|
||||
layoutConstant = Float(insets.left);
|
||||
break;
|
||||
case .Top:
|
||||
layoutConstant = Float(insets.top);
|
||||
break;
|
||||
case .Bottom:
|
||||
layoutConstant = Float(-insets.bottom);
|
||||
break;
|
||||
case .Right:
|
||||
layoutConstant = Float(-insets.right);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
func equalTo(attr: Any) -> Constraint {
|
||||
return self
|
||||
return equality(.Equal, attr: attr)
|
||||
}
|
||||
|
||||
func greaterThanOrEqualTo(attr: Any) -> Constraint {
|
||||
return self
|
||||
return equality(.GreaterThanOrEqual, attr: attr)
|
||||
}
|
||||
|
||||
func lessThanOrEqualTo(attr: Any) -> Constraint {
|
||||
return self
|
||||
return equality(.LessThanOrEqual, attr: attr)
|
||||
}
|
||||
|
||||
func insets(insets: Any) -> Constraint {
|
||||
|
|
|
@ -35,35 +35,35 @@ class ViewController: UIViewController {
|
|||
|
||||
let padding = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
|
||||
|
||||
view1.mas_makeConstraints({ make in
|
||||
make.top.and.left.greaterThanOrEqualTo(superview).insets(padding);
|
||||
view1.mas_makeConstraints { make in
|
||||
make.top.and.left.greaterThanOrEqualTo(superview).insets(padding)
|
||||
|
||||
make.bottom.equalTo(view3.mas_top).insets(padding);
|
||||
make.right.equalTo(view2.mas_left).insets(padding);
|
||||
make.width.equalTo(view2.mas_width);
|
||||
make.bottom.equalTo(view3.mas_top).insets(padding)
|
||||
make.right.equalTo(view2.mas_left).insets(padding)
|
||||
make.width.equalTo(view2.mas_width)
|
||||
|
||||
make.height.equalTo([view2, view3]);
|
||||
});
|
||||
make.height.equalTo([view2, view3])
|
||||
}
|
||||
|
||||
view2.mas_makeConstraints({ make in
|
||||
view2.mas_makeConstraints { make in
|
||||
// chain attributes
|
||||
make.top.and.right.equalTo(superview).insets(padding);
|
||||
make.top.and.right.equalTo(superview).insets(padding)
|
||||
|
||||
make.left.equalTo(view1.mas_right).insets(padding);
|
||||
make.bottom.equalTo(view3.mas_top).insets(padding);
|
||||
make.width.equalTo(view1.mas_width);
|
||||
make.left.equalTo(view1.mas_right).insets(padding)
|
||||
make.bottom.equalTo(view3.mas_top).insets(padding)
|
||||
make.width.equalTo(view1.mas_width)
|
||||
|
||||
make.height.equalTo([view1, view3]);
|
||||
});
|
||||
make.height.equalTo([view1, view3])
|
||||
}
|
||||
|
||||
view3.mas_makeConstraints({ make in
|
||||
make.top.equalTo(view1.mas_bottom).insets(padding);
|
||||
view3.mas_makeConstraints { make in
|
||||
make.top.equalTo(view1.mas_bottom).insets(padding)
|
||||
|
||||
// chain attributes
|
||||
make.left.right.and.bottom.equalTo(superview).insets(padding);
|
||||
make.left.right.and.bottom.equalTo(superview).insets(padding)
|
||||
|
||||
make.height.equalTo([view1, view2]);
|
||||
});
|
||||
make.height.equalTo([view1, view2])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue