Giter Club home page Giter Club logo

Comments (14)

s4cha avatar s4cha commented on May 14, 2024 2

@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.

s4cha avatar s4cha commented on May 14, 2024

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.

huang1988519 avatar huang1988519 commented on May 14, 2024

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.

s4cha avatar s4cha commented on May 14, 2024

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.

reeichert avatar reeichert commented on May 14, 2024

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.

telip007 avatar telip007 commented on May 14, 2024

That would be nice πŸ‘

from stevia.

huang1988519 avatar huang1988519 commented on May 14, 2024

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.

s4cha avatar s4cha commented on May 14, 2024

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.

waternotwords avatar waternotwords commented on May 14, 2024

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.

waternotwords avatar waternotwords commented on May 14, 2024

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.

s4cha avatar s4cha commented on May 14, 2024

@waternotwords that looks very promising!

It seems that we have to main use cases here :

  1. We just want to move one or two constraints and we would love to have accessors like label.topConstraint to modify the existing constraint
  2. 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.

s4cha avatar s4cha commented on May 14, 2024

@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.

reeichert avatar reeichert commented on May 14, 2024

@s4cha I tested this now, and works perfectly. thank you!

from stevia.

s4cha avatar s4cha commented on May 14, 2024

@reeichert great to hear !

from stevia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.