简体   繁体   中英

Add UIView to cover the whole UITableViewCell along with accessory view in iOS 10

I have a fairly easy custom cell with standard accessory view.

At some point I need to perform an animation of covering a whole cell with a colored view which has an image in its center. I am experiencing strange issues on iPhone 5 with iOS 10.2 while I am doing that.

This is what I do:

  1. Create subclass of UITableViewCell with nib file.

  2. Add a UIView coverView as a subview directly ( self.addSubview(self.coverView) ) to my custom cell (since I need to cover accessory view too, I can't add it to contentView ) in init methods. The coverView is created programmatically with no layout constraints. The UIImageView is added into it programmatically with layout constraints and translatesAutoresizingMaskIntoConstraints set to false .

  3. Add a method:

    func animateCState() {

     self.coverView.frame = self.bounds self.coverView.frame.origin.y = -self.frame.size.height self.bringSubview(toFront: self.coverView) UIView.animate(withDuration: 0.3, animations: { self.coverView.frame.origin.y = 0 }) 

    }

  4. Call animateCState when user taps on the cell

What I expect: green view with image in center slides from top

What I get: Apparently transparent view slides from top, and blinks to green almost at the end of animation. I can see imageView sliding in proper position.

Question: how to add a view which will cover the whole UITableViewCell (with accessory view) to a cell and be able to animate it?

Ok. It seems I have figured it out.

Apparently UITableView or UITableViewCell does something (set .clear color as background) to all views added directly to a cell either after prepareForReuse or at some other point in time. I had to set the color I want in my animateCState function right before I do animation.

So my method that works looks like this:

func animateCState() {

    self.coverView.frame = self.bounds
    self.coverView.frame.origin.y = -self.frame.size.height
    self.bringSubview(toFront: self.coverView)
    self.coverView.backgroundCOlor = .green
    UIView.animate(withDuration: 0.3, animations: {
        self.coverView.frame.origin.y = 0
    })  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM