Skip to content

How to set @QuickLayout view frame in UIViewController #7

Closed

Description

@zeroskylian

How to automatically set MyCellView frame

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
       // how to set MyCellView frame
        view.addSubview(MyCellView())
    }
}
@QuickLayout
class MyCellView: UIView {
    
    let titleLabel = UILabel(text: "titleLabel")
    let subtitleLabel = UILabel(text: "subtitleLabel")
    
    let imageView = UIImageView()
    
    var body: Layout {
        HStack {
            VStack(alignment: .leading) {
                titleLabel
                Spacer(4)
                subtitleLabel
                imageView.resizable()
            }
            Spacer()
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 8)
    }
}

Activity

  1. changed the title [-]How to set quick layout view frame in UIViewController[/-] [+]How to set @QuickLayout view frame in UIViewController[/+] on Dec 1, 2025
  2. constantine-fry commented on Dec 1, 2025

    @constantine-fry
    Contributor

    Hi @zeroskylian, thanks for trying QuickLayout. There are multiple way of achieving what you need:

    1. Define the body and call body.applyFrame in the viewDidLayoutSubviews.
    class ViewController: UIViewController {
    
      let cellView = MyCellView()
    
      override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(cellView)
      }
    
      var body: Layout {
        ZStack {
          cellView
        }
      }
    
      override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        body.applyFrame(view.bounds.inset(by: view.safeAreaInsets))
      }
    }

    You also can achieve that without QuickLayout as well just call the sizeThatFits and set the returned size on cellView.

    class ViewController: UIViewController {
    
      let cellView = MyCellView()
    
      override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(cellView)
      }
    
      override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        let cellSize = cellView.sizeThatFits(view.bounds.inset(by: view.safeAreaInsets).size)
        cellView.frame = CGRect(origin: .zero, size: cellSize)
      }
    }

    Let me know if you have any further questions. Cheers!

  3. constantine-fry commented on Dec 1, 2025

    @constantine-fry
    Contributor

    This is a very good question. Let me convert it to a discussion so other people can find it in the future. This issues will be closed and locked, but feel free to keep the conversation in the discussion.

  4. locked and limited conversation to collaborators on Dec 1, 2025
  5. converted this issue into a discussion #8 on Dec 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions