支持自动布局和动态高度的可关闭视图控制器

然而另一个滑动到关闭

向视图控制器添加滑动-消除逻辑的概念验证,支持自动布局和动态高度。

图像

Installation

This isn’t proper open-source code, so no package managers are supported yet. Just add the files from the to your project.PanelController

To make use of the behavior that provided, make sure your view controller conforms to and set its property to in your initializer. If you do this at a later stage, your view controller will not presented in a nice way.PanelControllerPanelPresentableviewControllerself

class SimpleViewController: UIViewController, PanelPresentable {
    
    let panelController = PanelController()
    
    init() {
        super.init(nibName: nil, bundle: nil)
        panelController.viewController = self
    }
}

Make sure to add your views to the property, which is forwarded to the view is ‘s scroll view. And any navigation-type views can be placed in the which will be displayed above your content and will stick to the top of the screen when scrolling.contentViewpanelControllertopContentView

    private lazy var simpleView: UIView = {
        let view = UIView()
        view.backgroundColor = .systemMint
        return view
    }()
    
    private lazy var cancelButton: UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Cancel", for: .normal)
        button.addAction(UIAction { [unowned self] _ in
            self.presentingViewController?.dismiss(animated: true)
        }, for: .touchUpInside)
        return button
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.tintColor = .black
        
        contentView.addSubview(simpleView)
        topContentView.addSubview(cancelButton)
        
        simpleView.translatesAutoresizingMaskIntoConstraints = false
        cancelButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            simpleView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
            simpleView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
            simpleView.topAnchor.constraint(equalTo: contentView.topAnchor),
            simpleView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
            simpleView.heightAnchor.constraint(equalToConstant: 200),
            
            cancelButton.leadingAnchor.constraint(equalTo: topContentView.layoutMarginsGuide.leadingAnchor),
            cancelButton.centerYAnchor.constraint(equalTo: topContentView.layoutMarginsGuide.centerYAnchor)
        ])
    }

Example code

In the repo you‘ll find that does something simular as the code showed above. A more complex example is where a bunch of random, multiline labels can be added to a . The height animates whenever the a label is added or removed. In the method an example is shown how to animate the height change by wrapping in an animation closure.SimpleViewControllerStackViewControllerUIStackViewanimateChanges()self.view.layoutIfNeeded()

Questions?

Look me up on twitter! ✌🏻

GitHub

点击跳转