Comments (14)
@reeichert @telip007 @waternotwords, the second case is done with the following :
// First layout
layout(
100,
|-email-|,
8,
|-password-|,
)
// Flush all view constraints
removeConstraints(constraints)
// Re-apply different layout
layout(
|-password-|,
8,
|-44-email-100-|,
10
)
// Animate if we want to
UIView.animateWithDuration(2) {
self.layoutIfNeeded()
}
from stevia.
Hey Huang,
As you may know the usual way to change a constraint is to change the constant
property on it.
This is how you would do it natively :
detailTopConstraint = NSLayoutConstraint(item: detail, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: 20)
addConstraint(detailTopConstraint)
// Later when you want to change the margin for example :
detailTopConstraint.constant = 100
With Stevia it becomes :
detailTopConstraint = c(item: detail, attribute: .Top, toItem: self, constant: 20)
// Later
detailTopConstraint.constant = 100
As you see the c()
enables you to be more concise as it fills default parameters plus it calls the addConstraint()
for you.
In case the .constant
doesn't suffice you would have to store it and indeed, remove it to replace it with a new one.
from stevia.
Review code, "c" methed is a methed to change a constraint. But it`s a regular way. Could use costom operator like "-|, |- ,~" to update or remake a exist constraint ?
from stevia.
That's a very could point, at the moment |-label
returns the label and
|-label-img-|
returns the array making it easy to chain methods. So there is actually no way to get back the constraint created. If you have any creative ideas please let us know :)
from stevia.
There is no way to remake constraints? any method like
view.remakeLayout([
100,
|-email-| ~ 80,
8,
|-password-| ~ 80,
"",
|login| ~ 80,
0
])
or
view.updateLayout([
100,
|-email-| ~ 80,
8,
|-password-| ~ 80,
"",
|login| ~ 80,
0
])
and then call self.layoutIfNeeded()
from stevia.
That would be nice π
from stevia.
Stevia is more and more strong .Stevia can resolve my layout appeal layout cases .
Here is my advice:
But review newest source code and Test Cases , complexer, I can not use one style of code to layout a UIView. Layout([]) methed can work. top, bottom, right...alse can work and have more public methed to user.
I expect to user 'objc_getAssociatedObject' to bind constraint, then I can get this constraint to modify, remove and so on by '|- -| ~ '.
My English is bad, If the expression is not accurate, please forgive me.
from stevia.
Hey guys,
Sorry for the late reply, just want to let you know I'm actively thinking about this request and I'll get back to you as soon as I get something valuable π
from stevia.
Stevia Rocks
200 times more sweet than any NSAutoLayout syntactic sugar I've seen!!!
It would be nice if there also was some sort of syntactic sugar for auto creation of identifiers from constraint types and a programmer provided String, which would solve many of the later adjustment needed for modern view animation when dealing with a lot of views.
from stevia.
Using an NSLayoutConstraint extension to automatically add a human readable identifier for each constraint with an UIView extension including computed properties that return the appropriate constraints would make Stevia more useful for manipulating constraints:
extension NSLayoutConstraint {
func setAutoIdentifier(){
self.identifier = NSLayoutConstraint.getAutoIdentifierString(forView: self.firstItem, type: self.firstAttribute)
}
static func getAutoIdentifierStringForView(viewObject:AnyObject, type:NSLayoutAttribute) -> String {
let objID = ObjectIdentifier(viewObject).uintValue
return "\(viewObject)-\(objID)-\(type.string)Constraint"
}
}
This could be combined with a UIView extension to make it super easy to adjust layouts:
extension UIView {
func getConstraintByType(type:NSLayoutAttribute)->NSLayoutConstraint? {
// get the identifier
// itterate through constraints & check for identifier
// return constraint if it exists
}
var constraintLeft:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Left) }
var constraintRight:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Right) }
var constraintTop:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Top) }
var constraintBottom:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Bottom) }
var constraintLeading:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Leading) }
var constraintTrailing:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Trailing) }
var constraintWidth:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Width) }
var constraintHeight:NSLayoutConstraint? { return getConstraintByType(NSLayoutAttribute.Height) }
// etc.....
}
from stevia.
@waternotwords that looks very promising!
It seems that we have to main use cases here :
- We just want to move one or two constraints and we would love to have accessors like
label.topConstraint
to modify the existing constraint - We want to change the whole layout at once and in this case the flush & relayout seems to be the simplest approach
I am going to test implementations of those two strategies this weekend and let you guys know how it goes :)
from stevia.
@huang1988519 @waternotwords @reeichert @telip007
A solution to the first case (modifying an existing constraint) is available on "get_constraint" branch π.
It's pretty much just this code (btw thanks a ton to @waternotwords who did all the work here π)
public extension UIView {
public var leftConstraint:NSLayoutConstraint? {
return constraintForView(self, attribute: .Left)
}
public var rightConstraint:NSLayoutConstraint? {
return constraintForView(self, attribute: .Right)
}
public var topConstraint:NSLayoutConstraint? {
return constraintForView(self, attribute: .Top)
}
public var bottomConstraint:NSLayoutConstraint? {
return constraintForView(self, attribute: .Bottom)
}
public var heightConstraint:NSLayoutConstraint? {
return constraintForView(self, attribute: .Height)
}
public var widthConstraint:NSLayoutConstraint? {
return constraintForView(self, attribute: .Width)
}
}
func constraintForView(v:UIView, attribute:NSLayoutAttribute) -> NSLayoutConstraint? {
if let spv = v.superview {
for c in spv.constraints {
if let fi = c.firstItem as? NSObject where fi == v && c.firstAttribute == attribute {
return c
}
if let si = c.secondItem as? NSObject where si == v && c.secondAttribute == attribute {
return c
}
}
}
return nil
}
And a minor adjustment to width and height constraints that were previously added on the view itself rather than their superview
private func size(attribute:NSLayoutAttribute, points:CGFloat) -> UIView {
if let spv = superview {
let c = constraint(item: self, attribute:attribute, constant: points)
spv.addConstraint(c)
}
return self
}
Now you can write
image.height(100)
// And later on
image.heightConstraint?.constant = 200
// Animate if we want to
UIView.animateWithDuration(2) {
self.layoutIfNeeded()
}
Just tried on our own app and it looks like it does the job.
Let me know how it goes for you guys :)
Once everybody agrees we'll add tests and add in to master !!
from stevia.
@s4cha I tested this now, and works perfectly. thank you!
from stevia.
@reeichert great to hear !
from stevia.
Related Issues (20)
- Help, layout constraints like SnapKit HOT 2
- Stick two views with space between HOT 4
- Example for UIScrollView HOT 1
- Can't add the Stevia from swift package manager HOT 2
- Set left, right edges space to more than safe area HOT 2
- Additional operators for LayoutAnchors? HOT 2
- Working with Safe Area Layout Guide and Operators HOT 5
- Swift Package Manager dependency resolution fails HOT 3
- UICollectionViewCell & UITableViewCell iOS 13. Add subviews in contentView.
- Value of type 'Button' has no member 'style'
- SteviaLayoutX/YAxisAnchor doesn't support inequality operators? HOT 1
- LastBaseline crash
- @_functionBuilder
- Version compatibility issue in 5.1.1 with swift 5.5 Xcode 13.1 HOT 4
- Height equal height with multiplier. HOT 1
- using layout with safeAreaLayoutGuide
- SwiftUI preview fails with error: could not find module 'Stevia' for target 'x86_64-apple-ios-simulator'; found: arm64-apple-ios-simulator
- Support for cornerRadius
- Multiplier in Constraints
- Different behaviour with same constraint
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from stevia.