Search code examples
uitableviewfocustvos

Don't use default focus animation for UITableViewCell


I have a very simple cell and I want the text to be 1.0 alpha when focused and 0.5 when unfocused. This works by overriding didUpdateFocusInContext

import Foundation

class SettingsTableViewCell: UITableViewCell {


override func didUpdateFocusInContext(context: UIFocusUpdateContext, withAnimationCoordinator coordinator: UIFocusAnimationCoordinator) {
    super.didUpdateFocusInContext(context, withAnimationCoordinator: coordinator)

    var textColor:UIColor?

    if self.focused == true {
        textColor = UIColor.blackColor().colorWithAlphaComponent(1.0)
    } else {
        textColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
    }

    let attributedTitle = self.textLabel?.attributedText?.mutableCopy() as! NSMutableAttributedString
    let range =  NSMakeRange(0, attributedTitle.length)
    attributedTitle.addAttribute(NSForegroundColorAttributeName, value: textColor!, range: range)
    self.textLabel?.attributedText = attributedTitle

}

}

HOWEVER, I don't want the white background when focused... how to get rid of it?

enter image description here


Solution

  • Set your cells' focusStyle to UITableViewCellFocusStyleCustom and UIKit won't do any default focus appearance at all on the cells: it'll be entirely up to you.