Giter Club home page Giter Club logo

Comments (5)

s4cha avatar s4cha commented on May 13, 2024

@biskit it does set both indeed :) maybe a part of the code overrides your height?

from stevia.

biskit avatar biskit commented on May 13, 2024

checked, it does not. however if have something like this:
button: a, b, c
view1.sv(b,c).layout(5,|-b-|,5,|-c-|)
sv(a, view1).layout(5,|-a-view1-|)

ie. layout b and c into view1 vertically and then layout a and view1 side by side.
now if I size a.size(width_2/3) and view1.size(width_1/3) it does not affect height.

what are the rules to laying out a view within a view. all your samples are simple and in real world too simple...

from stevia.

biskit avatar biskit commented on May 13, 2024

also, why does it not change the frame size of the holding view to bound all its children? as you know if the views frame is not set, then events do not pass to the children. it has to be done manually and i would have thought otherwise.

from stevia.

s4cha avatar s4cha commented on May 13, 2024

Hi @biskit ,

Here is an implementation of your issue :

import Stevia

class BizkitView:UIView {

    let a = UIButton()
    let view1 = UIView()
    let b = UIButton()
    let c = UIButton()

    convenience init() {
        self.init(frame:CGRectZero)

        // Hierarchy
        sv(
            a,
            view1
        )
        view1.sv(
            b,
            c
        )

        // Layout
        layout(
            5,
            |-a-view1-|
        )
        view1.layout(
            5,
            |-b-|,
            5,
            |-c-|,
            5 // 1. Missing bottom constraint
        )

        // 2. Apply 2/3 ratio on "a"
        addConstraint(
            NSLayoutConstraint(item: a,
                attribute: .Width,
                relatedBy: .Equal,
                toItem: self,
                attribute: .Width,
                multiplier: 2.0/3.0,
                constant: 1
            )
        )

        // Styling
        backgroundColor = .whiteColor()
        a.backgroundColor = .redColor()
        a.setTitle("a", forState: .Normal)
        view1.backgroundColor = .blueColor()
        b.backgroundColor = .greenColor()
        b.setTitle("b", forState: .Normal)
        c.backgroundColor = .yellowColor()
        c.setTitle("c", forState: .Normal)
    }
}

1. Missing constraint

Without this bottom constraint, view1 would have no height at all, and would be invisible as you described well above. Thanks to this, it will stretch to whatever b and c heights are, currently UIBUtton's native instrisic height.

2. Ratio

Setting a 2/3 ratio on a will automatically set view1 to take the 1/3 remaining space, your layout being |-a-view1-|
Trying to set both would invariably produce an ambiguous layout with conflicting extra constraints :)

As a side note, it's a (very) good practice to never reference frame size or harcoded screen values. That's kind of the whole purpose of Autolayout :p

What's more we need to be aware that the frame is not set at this time !, rightfully so since it's just being inited and the Autolayout Pass has not happend yet.

Finally, view.width(X) only works with hardocoded values. I guess what you would like here is a ratio instead, that is calculated at runtime thanks to autolayout and the current screen width, and not a computed value based on the current frame.

Bonus 1 🎉

Want a and view1 to have the same height ?

equalHeights(a,view1)

Bonus 2 🎉

With this extension

extension UIView {
    func widthRatio(ratio:CGFloat) -> UIView {
        if let spv = superview {
            spv.addConstraint(
                NSLayoutConstraint(
                    item: self,
                    attribute: .Width,
                    relatedBy: .Equal,
                    toItem: spv,
                    attribute: .Width,
                    multiplier: ratio,
                    constant: 1
                )
            )
        }
        return self
    }
}

You can replace

addConstraint(  
  NSLayoutConstraint(
    item: a,  
    attribute: .Width,  
    relatedBy: .Equal,
    toItem: self,
    attribute: .Width,
    multiplier: 2.0/3.0,
    constant: 1)  
)

with

a.widthRatio(2/3)

Side note

Complex nested views

We believe complex nested view should be refactored into their own UIView subclasses. In your case it's pretty simple layout but in more complex cases it becomes harder to read and maintain when the whole layout is dumped in one giant UIVIew file :)

Stevia / Autolayout for real world Layouts

Stevia is just a handy overlay on top of Autolayout so you can do everything that Autolayout does. The issue highlithed above is actually an Autolayout issue and not really Stevia related per se.
Autolayout in code can seem tricky at first (we've all become kinda lazy with the IB WYSIWYG :p), but understanding how it works comes with great joy!
By the way our App (Yummypets) has absolutely no storyboards and all the views and cells, we have tons of them, are all built using Stevia ;)

I really hope this helps you solves your issue :)

from stevia.

biskit avatar biskit commented on May 13, 2024

thanks a bunch for details!

the revelation here is point 1. that's what it took me to pull the view over the items inside it.
so, just to make sure it covers the items in the view, i set the bottom value to 0.

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.