Coder Social home page Coder Social logo

Comments (3)

lkzhao avatar lkzhao commented on May 23, 2024

This is expected. In this framework, there are two phases when rendering. One is the layout, second is rendering the views that are on screen.
The component view will first layout the entire component tree, then only create the views that are on screen. This makes it fast just like a UICollectionView where even if you have 100000 cell, it will still only display the ones that user scrolls to.

SimpleViewComponent by default, doesn't initialize a view for it to get its size. If you imagine having 100,000 of these in your feed, if each SimpleViewComponent initialize a view to retrieve its size, it is going to take a lot of memory and be very slow to initialize. So normally you will have to supply a size via the .size() modifier, or do your own size calculation by writing your own component.

SimpleViewComponent<UIButton>().update {
    $0.setTitle("button", for: .normal)
    $0.setTitleColor(.systemBlue, for: .normal)
}.backgroundColor(.brown).size(CGSize(width:100, height:44))

However, if you don't update your feed frequently, or if you only have a few of these views. You can directly create a view and pass it into the SimpleViewComponent like so:

let button = UIButton()
button.setTitle("button", for: .normal)
button.setTitleColor(.systemBlue, for: .normal)
SimpleViewComponent(view: button)

This way, the SimpleViewComponent has a view to retrieve the size from, and you will get the correct layout.

And did you know? all UIView are compatible to be used as a ViewComponent.
So what I do normally if I have a few views that I would like to display in a componentView is the following:

class ViewController: UIViewController {
  let button = UIButton()
  // ...
  func viewDidLoad() {
    super.viewDidLoad()
    button.setTitle("button", for: .normal)
    button.setTitleColor(.systemBlue, for: .normal)
    componentView.component = VStack {
      // ...
      button // <- notice that I can directly put the button inside any layout component, this will turn it into a SimpleViewComponent automatically
    }
}

from uicomponent.

lkzhao avatar lkzhao commented on May 23, 2024

If you are going to build a scrolling feed of these cells, you should create your own component for your cells. You can check out the Card examples or the Text or Image component to see how the size is calculated during a layout cycle if you plan to implement your own Component.

from uicomponent.

wwdc14yh avatar wwdc14yh commented on May 23, 2024

This is awesome and very helpful to me

from uicomponent.

Related Issues (16)

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.