Gilad Schickler
Gilad Schickler

Reputation: 135

animate UIStackView to slide in from top

I have a navigation bar with a BarButton that should act as a menu button set up in story board.

When its clicked i have a function creating a stack view with the menu items (UIButtons) as its arranged subviews. In addition constraints for top, bottom, trailing and leading are added through a function called from within the createStackView function.

Here is the code:

let menuArray : [String] = ["a", "b" , "c", "d"]

func buttonWtitle(title : String) -> UIButton {
    let button = UIButton()
    button.setTitle(title, for: .normal)
    button.backgroundColor = UIColor.lightGray
    return button
}

func createButtons() -> [UIButton]{
    var menu = [UIButton]()
    for title in menuArray{
        menu.append(buttonWtitle(title: title))
    }
    return menu
}

    func createMenu(parentview : UIView){
    let buttonArray = createButtons()
    let stackView = UIStackView(arrangedSubviews: buttonArray)
    stackView.axis = .vertical
    stackView.distribution = .fillEqually
    stackView.alignment = .fill

    addSubviewWithConstraints(to: parentview, and: stackView, top: 0 , bottom:0, leading: 0, trailing: 0) //this function adds constraints   
}

This is in its own class and its called from ViewController by the bar button.

Everything is working fine but I am trying to animate it so it comes from the top of the screen. Is there a way to do that with a stackView?

Upvotes: 2

Views: 2407

Answers (1)

sschmaljohann
sschmaljohann

Reputation: 66

You may want to look into this: How do I animate constraint changes?

With that you can create the UIStackView and hide it by setting its bottom-constraint to the superview's top-constraint. Then you can animate the change to your current constraints.

Upvotes: 3

Related Questions