Skip to content

Cannot display "UIViewController with dynamic height" #392

@GengJian

Description

@GengJian

Describe the bug
Here is a UIViewController, mainly UIScrollView wrapped UIStackView, please note that he is the dynamic height;

if you directly display this "StackViewController" is no problem,

but because of business needs, in order to perform the jump then you need to use "UINavigationController.initWithRoot:StackViewController" , at this time can only display a fixed 44 height of the pop-up window, , look forward to fixing!

Expected behavior
display a UINavigationController with a DynamicHeightViewController

Screenshots
If applicable, add screenshots to help explain your problem.

iPhone (please complete the following information):

  • Device: iPhone13 Pro Max
  • iOS Version: iOS 17.4.1
  • Xcode Version 15
  • Dependency Manager Version [e.g. CocoaPods 1.5.2, Carthage 0.29.0]
  • SwiftEntryKit Release # [e.g. 0.2.0]

Additional context

import UIKit
import SnapKit

class StackViewController: UIViewController {

let scrollView = UIScrollView()
let stackView = UIStackView()
let toggleButton = UIButton()
var isHidden = false

override func viewDidLoad() {
    super.viewDidLoad()

    // 配置 scrollView
    scrollView.backgroundColor = .yellow
    view.addSubview(scrollView)
    scrollView.snp.makeConstraints { make in
        make.edges.equalTo(view.safeAreaLayoutGuide)
    }

    // 配置 stackView
    stackView.axis = .vertical
    stackView.distribution = .fill
    stackView.spacing = 0
    scrollView.addSubview(stackView)
    stackView.snp.makeConstraints { make in
        make.edges.equalToSuperview()
        make.width.equalToSuperview()
    }

    // 获取屏幕高度
    let screenHeight = UIScreen.main.bounds.height

    // 添加子视图到 stackView
    for i in 1...16 {
        let subview = UILabel()
        subview.text = "\(i)"
        subview.tag = i // 为每个子视图设置 tag 以便识别
        subview.backgroundColor = i % 2 == 0 ? .red : .blue // 交替颜色便于区分
        stackView.addArrangedSubview(subview)

        // 设置高度约束
        subview.snp.makeConstraints { make in
            make.height.equalTo(screenHeight / 10).priority(.required)
        }
    }

    // 配置按钮
    toggleButton.setTitle("Toggle Odd Rows", for: .normal)
    toggleButton.backgroundColor = .systemBlue
    toggleButton.addTarget(self, action: #selector(toggleOddRows), for: .touchUpInside)
    view.addSubview(toggleButton)

    toggleButton.snp.makeConstraints { make in
        make.bottom.equalTo(view.safeAreaLayoutGuide).offset(-20)
        make.centerX.equalToSuperview()
        make.width.equalTo(200)
        make.height.equalTo(50)
    }
}

@objc func toggleOddRows() {
    isHidden.toggle()

    UIView.animate(withDuration: 0.5, animations: {
        for i in 1...16 {
            if let subview = self.stackView.viewWithTag(i) {
                if i % 2 != 0 { // 单数行
                    subview.isHidden = self.isHidden
                }
            }
        }
        self.stackView.layoutIfNeeded() // 强制刷新 stackView 布局
    }) { _ in
        // 更新 scrollView 内容高度
        self.scrollView.setNeedsLayout()
        self.scrollView.layoutIfNeeded()
    }
}

}

===============

var attributes = EKAttributes()
    attributes.position = .bottom
    attributes.displayDuration = .infinity
    attributes.entryBackground = .color(color: EKColor(UIColor.white))
    attributes.screenBackground = .color(color: EKColor(UIColor.black.withAlphaComponent(0.5)))
    attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 8))
    attributes.roundCorners = .all(radius: 10)
    attributes.positionConstraints.size = .init(width: .offset(value: 20), height: .intrinsic)
    
    let dynamicHeightVC = StackViewController()
    let nav = UINavigationController(rootViewController: dynamicHeightVC)
    SwiftEntryKit.display(entry: nav, using: attributes)

Screenshots / Video Links
image

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions