-1

All,

I want to create a drop down that stores UIScrollViews etc

I have done a lot of this by using the animation block - from x = 30 to x = 400 , so it brings the box down. when the success block runs, it turns the UIScollview from hidden = false .. So it displays the UIScoll views after the completed animation.

Is this the best way ? I am trying to acheieve these results

enter image description here

This is the filter exposed . so the animation runs to bring the blue down and then I unhide the scrollviews.

This is the image before, when the filter is not exposed. enter image description here

So there is a blue bit underneath the NAV bar.

Can anyone help me here, or is this the back practice ?

swiftBoy
  • 35,607
  • 26
  • 136
  • 135
Jason
  • 1,057
  • 3
  • 13
  • 31

2 Answers2

2

the question is bit old now but if anyone coming for the same. I would suggest must have a look at this article

Menu interface: Controller Libraries Please Thanks to original author Manoolia ;)

and Awesome-iOS git repo

enter image description here

and one of my favorite is Yalantis/GuillotineMenu

Sample Code

Just create MenuViewController

   class MenuViewController: UIViewController, GuillotineMenu {
    //GuillotineMenu protocol
    var dismissButton: UIButton!
    var titleLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        dismissButton = UIButton(frame: CGRectZero)
        dismissButton.setImage(UIImage(named: "ic_menu"), forState: .Normal)
        dismissButton.addTarget(self, action: "dismissButtonTapped:", forControlEvents: .TouchUpInside)

        titleLabel = UILabel()
        titleLabel.numberOfLines = 1;
        titleLabel.text = "Activity"
        titleLabel.font = UIFont.boldSystemFontOfSize(17)
        titleLabel.textColor = UIColor.whiteColor()
        titleLabel.sizeToFit()
    }

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("Menu: viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        print("Menu: viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        print("Menu: viewWillDisappear")
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        print("Menu: viewDidDisappear")
    }

    func dismissButtonTapped(sende: UIButton) {
        self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    }

    @IBAction func menuButtonTapped(sender: UIButton) {
        self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    }

    @IBAction func closeMenu(sender: UIButton) {
        self.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
    }

}

extension MenuViewController: GuillotineAnimationDelegate {
    func animatorDidFinishPresentation(animator: GuillotineTransitionAnimation) {
        print("menuDidFinishPresentation")
    }
    func animatorDidFinishDismissal(animator: GuillotineTransitionAnimation) {
        print("menuDidFinishDismissal")
    }

    func animatorWillStartPresentation(animator: GuillotineTransitionAnimation) {
        print("willStartPresentation")
    }

    func animatorWillStartDismissal(animator: GuillotineTransitionAnimation) {
        print("willStartDismissal")
    }
}

and use above in your View-controller this way

class ViewController: UIViewController {

    let reuseIdentifier = "ContentCell"
    private let cellHeight: CGFloat = 210
    private let cellSpacing: CGFloat = 20
    private lazy var presentationAnimator = GuillotineTransitionAnimation()

    @IBOutlet var barButton: UIButton!

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        print("VC: viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        print("VC: viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        print("VC: viewWillDisappear")
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
        print("VC: viewDidDisappear")
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        let navBar = self.navigationController!.navigationBar
        navBar.barTintColor = UIColor(red: 65.0 / 255.0, green: 62.0 / 255.0, blue: 79.0 / 255.0, alpha: 1)
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
    }

    @IBAction func showMenuAction(sender: UIButton) {
        let menuVC = storyboard!.instantiateViewControllerWithIdentifier("MenuViewController")
        menuVC.modalPresentationStyle = .Custom
        menuVC.transitioningDelegate = self
        if menuVC is GuillotineAnimationDelegate {
            presentationAnimator.animationDelegate = menuVC as? GuillotineAnimationDelegate
        }
        presentationAnimator.supportView = self.navigationController?.navigationBar
        presentationAnimator.presentButton = sender
        presentationAnimator.duration = 0.6
        self.presentViewController(menuVC, animated: true, completion: nil)
    }
}

// The following is just for the presentation. You can ignore it
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell: AnyObject? = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        return cell as! UICollectionViewCell!
    }

    func collectionView(collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(CGRectGetWidth(collectionView.bounds) - cellSpacing, cellHeight)
    }
}

extension ViewController: UIViewControllerTransitioningDelegate {

    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        presentationAnimator.mode = .Presentation
        return presentationAnimator
    }

    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        presentationAnimator.mode = .Dismissal
        return presentationAnimator
    }
}
swiftBoy
  • 35,607
  • 26
  • 136
  • 135
1

If you are targeting iOS 7 or later you can achieve this by putting two height constrains in your drop down view with different priorities lets say for example priority 750 and constant of drop down height for state shown and 700 priority and constant of 0 for state hidden. Next in order to to show/hide drop down just call the following functions :

func showView(dropDownView: UIView) {

    let constrains = (dropDownView.superview?.constraints() as [NSLayoutConstraint]) +
            (dropDownView.constraints() as [NSLayoutConstraint])
    for constrain  in constrains{

        if(constrain.priority == 650{

            constrain.priority = 750

       }

    }

    dropDownView.hidden = false
    UIView.animateWithDuration(0.4, animations: {
        dropDownView.alpha = 1
        dropDownView.superview?.layoutIfNeeded()
        }, completion: {
        (value: Bool) in

    })


}

func hideView(dropDownView: UIView) {

    let constrains = (dropDownView.superview?.constraints() as [NSLayoutConstraint]) +
            (dropDownView.constraints() as [NSLayoutConstraint])
    for constrain  in constrains{

        if(constrain.priority == 750{

            constrain.priority = 650

       }

    }

    UIView.animateWithDuration(0.4, animations: {
        dropDownView.alpha = 0
        dropDownView.superview?.layoutIfNeeded()
        }, completion: {
        (value: Bool) in
        dropDownView.hidden = true                       
    })


}
Zell B.
  • 10,266
  • 3
  • 40
  • 49
  • Hi Zelb, I am using ios7+, I think I should look at NSLayoutConstrait, as I am not too sure what it is. – Jason Jan 14 '15 at 13:28