LC 웃
LC 웃

Reputation: 18998

Table View Animation not working properly while adding ViewController on a Container View?

I have currently set up my tableview on the left side of the screen so that I can animate it from there as below:

override func viewDidAppear(animated: Bool) {
    tblView.frame = CGRectMake(-self.view.frame.size.width, 48, self.tblView.frame.size.width, self.tblView.frame.size.height)
}

Now I have set up a button that animates the tableview from - self.view.frame.size.width to 0 as below and this works fine:

@IBAction func showMenu(sender: AnyObject) {
    self.tblView.hidden = false    
    if flag == false{
        showMenu()
    } else {
        hidemenu()
    }
}

func showMenu(){
    UIView.animateWithDuration(0.3, animations:{
        self.tblView.frame = CGRectMake(0, 48, self.tblView.frame.size.width, self.tblView.frame.size.height)
    } )
    flag = true
}

func hidemenu(){
    UIView.animateWithDuration(0.3, animations:{
        self.tblView.frame = CGRectMake(-self.view.frame.size.width, 48, self.tblView.frame.size.width, self.tblView.frame.size.height)
    } )
    flag = false
}

The problem is when I am trying to hide the menu after clicking on tableview cell as below:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    var row = indexPath.row
    var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    switch row {
    case 0:           
        hidemenu()
        println("hide menu")
        if firstVC == nil{
            firstVC  = storyboard.instantiateViewControllerWithIdentifier("firstVC") as? FirstViewController
        }
        activeViewController = firstVC
    default:
        println("no index")
    }
}

The tableview does not get hide towards the left instead the animation appears to be from right and ends in the 0. Instead I am seeking the animation from 0 to -self.view.frame.size.width position on x-axis.

hideMenu function gets called.

EDITED: After hours of debugging with None i found that my error is on the line when in did select method i assign the view controller to the content view of the containerView

  func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        hidemenu()
        var row = indexPath.row
        var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        switch row {                
        case 0:             

            if firstVC == nil{

                firstVC  = storyboard.instantiateViewControllerWithIdentifier("firstVC") as? FirstViewController
            }                
            activeViewController = firstVC
.....           
}

I have variable declare as

 var activeViewController : UIViewController? {            
        didSet{
            removeInactiveViewController(oldValue)
            updateActiveViewController()
        }
    }

  func updateActiveViewController(){            

        if let activeVC = activeViewController{

            activeVC.view.frame = contentView.bounds
            contentView.addSubview(activeVC.view)//this line gives me error
            activeVC.didMoveToParentViewController(self)

        }

    }

Going to drill down the error in the animation is while adding the activeVC on the content view as contentView.addSubview(activeVC.view). Sorry for lengthy post ..

If i comment the line the animation works fine..But the view is not added to my main view...How do i fix this?

Upvotes: 0

Views: 621

Answers (2)

Jatin Patel - JP
Jatin Patel - JP

Reputation: 3733

Actually you are applying animation by setting frame manually and you are already set AutoLayout constraints. so this is conflict internally. kindly removed your UITableView constraints from storyboard and it working fine.

Initial

enter image description here

and after removing constraints, it will look like.

enter image description here

Hope this help you.

Upvotes: 1

ak2g
ak2g

Reputation: 1673

 func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

        var row = indexPath.row
        var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)


        self.tblView.hidden = true
        flag = false
         ........
}

and change this as

 @IBAction func showMenu(sender: AnyObject) {

        if self.tblView.hidden == true{
            self.tblView.frame = CGRectMake(-500, 48, self.tblView.frame.size.width, self.tblView.frame.size.height)

        }

        self.tblView.hidden = false

        if flag == false{

            displayMenu()

        }else{

          hidemenu()

        }

    }

Upvotes: 0

Related Questions