freshOS / Stevia

:leaves: Concise Autolayout code
https://freshos.github.io/SteviaDocs/
MIT License
3.38k stars 213 forks source link

Unintended CenterY #171

Open didebbo opened 1 month ago

didebbo commented 1 month ago

Screenshot of code Screenshot of view debug

As you can see, without explicitly specifying the CenterY constraint, it is still being applied to the subviews, causing this unintended centralized effect.

Using the layout: align(tops: |-10--boxOne--10--boxTwo--10-|), boxTwo gets stretched and no longer takes its height based on its content.

It would be ideal to have additional layout options such as anchorTo(.top | .bottom | .leading | .trailing).

s4cha commented 1 month ago

Hi @didebbo, could you please provide the entire layout code reproducing the issue please ? (including views creating & hierarchy).

s4cha commented 1 month ago

@didebbo, this is the default behavior indeed, when using multiple horizontal views in a layout block, they are aligned horizontally. One catch here is that the top and bottom constraints are only applied to the first view of the horizontal layout, here box1. In your case, you might be better off using other apis. Here is an example below. If you want to provide the exact layout desired, I can help you with it. In the meantime, here is a rough implementation.


import UIKit
import Stevia

class View: UIView {

    let box1 = UITextView()
    let box2 = UITextView()

    convenience init() {
        self.init(frame: CGRect.zero)

        // 1 - Hierarcy
        subviews {
            box1
            box2
        }

        // 2 - Layout
        box1.top(10).bottom(0)
        |-10-box1.width(>=100)-box2-10-|

        // Provide default height when box is empty
        box2.height(>=100)

        // Match box 1 top
        box2.Top == box1.Top

        // Match box1 width
        box2.Width == box1.Width

        // Stop growing at the bottom of the screen
        box2.Bottom <= 10

        // 3 -Style
        box1.style { v in
            v.backgroundColor = .red
        }

        box2.style { v in
            v.backgroundColor = .blue

            // Fit content and grow with it
            v.isScrollEnabled = false
        }

    }
}

class ViewController: UIViewController {

    let v = View()
    override func viewDidLoad() {
        super.viewDidLoad()

        v.box1.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."

        v.box2.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
    }

    override func loadView() {
        self.view = v
    }
}

Simulator Screenshot - iPhone 15 Pro - 2024-09-12 at 08 00 34