UITableView阻尼动画和布局约束

时间:2015-11-16 16:19:18

标签: ios iphone uitableview constraints ios-animations

我正在尝试通过使用其高度约束和UIView.animateWithDamping(..)块来为UITableView设置动画,使其像dropdownMenu一样。我在tableView下遇到了白色背景的奇怪问题。

iPhone Simulator showing the problem

我清除了每种背景颜色并没有多大帮助。
下面是设置dropDownView的所有子视图的代码,它是一个UIView:

required public init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self.elements = []
    defaultSetup()
}

private func defaultSetup() {
    configureActionButton()
    configureTableView()
}
private func configureActionButton() {
    actionButton = UIButton(frame: CGRectZero)
    actionButton.translatesAutoresizingMaskIntoConstraints = false
    addSubview(actionButton)
    guard let superview                                 = actionButton.superview else {
        assert(false, "ActionButton adding to superview failed.")
        return
    }
    // Constraints
    actionButton.constrain(.Leading, .Equal, superview, .Leading, constant: 0, multiplier: 1)?.constrain(.Trailing, .Equal, superview, .Trailing, constant: 0, multiplier: 1)?.constrain(.Top, .Equal, superview, .Top, constant: 0, multiplier: 1)?.constrain(.Bottom, .Equal, superview, .Bottom, constant: 0, multiplier: 1)
    // Appearance
    actionButton.backgroundColor                        = UIColor.clearColor()
    actionButton.opaque                                 = false
    actionButton.contentHorizontalAlignment             = .Left
    actionButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
    if borderVisible {
        actionButton.layer.cornerRadius                 = 5
        actionButton.layer.borderColor                  = UIColor.blackColor().CGColor
        actionButton.layer.borderWidth                  = 1
        actionButton.clipsToBounds                      = true
    }
    // Actions
    actionButton.addTarget(self, action: "menuAction:", forControlEvents: .TouchUpInside)
}

private func configureTableView() {
    tableView                                           = BOTableView(frame: CGRectZero, items: elements, configuration: configuration)
    tableView.translatesAutoresizingMaskIntoConstraints = false
    tableView.delegate                                  = self
    tableView.dataSource                                = self
    addSubview(tableView)
    guard let tableViewSuperview = tableView.superview else {
        assert(false, "TableView adding to superview failed.")
        return
    }
    // Constraints
    tableView.constrain(.Trailing, .Equal, tableViewSuperview, .Trailing, constant: 0, multiplier: 1)?.constrain(.Top, .Equal, tableViewSuperview, .Bottom, constant: 0, multiplier: 1)?.constrain(.Leading, .Equal, tableViewSuperview, .Leading, constant: 0, multiplier: 1)
    tvHeightConstraint                                  = NSLayoutConstraint(item: tableView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 0)
    tableView.addConstraint(tvHeightConstraint)

}

BOTableView类初始化程序:

init(frame: CGRect, items: [String], configuration: BOConfiguration) {
    super.init(frame: frame, style: UITableViewStyle.Plain)

    self.items                              = items
    self.selectedIndexPath                  = NSIndexPath(forRow: 0, inSection: 0)
    self.configuration                      = configuration

    // Setup table view
    self.opaque                             = false
    self.backgroundView?.backgroundColor    = UIColor.clearColor()
    self.backgroundColor                    = UIColor.clearColor()
    self.separatorColor                     = UIColor.blackColor()
    self.scrollEnabled                      = false
    self.separatorStyle                     = .SingleLine

    self.layer.cornerRadius                 = 5
    self.layer.borderColor                  = UIColor.blackColor().CGColor
    self.layer.borderWidth                  = 1
    self.clipsToBounds                      = true
}

UIView动画:

private func showMenuWithCompletionBlock(completion: (succeeded: Bool) -> Void) {
    delegate?.menuWillShow(self)
    let tvHeight                            = frame.size.height * CGFloat(elements.count)
    tvHeightConstraint.constant             = tvHeight

    UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.5, options: .CurveEaseInOut, animations: { [weak self] () -> Void in
            guard let strongSelf = self else {
                completion(succeeded: false)
                return
            }
            strongSelf.layoutIfNeeded()
            }, completion: { (finished) -> Void in
                if finished {
                    completion(succeeded: true)
                }
        })
}

以下是UIView + Constraints扩展的代码,用于代码:

extension UIView {
/**
 :returns: true if v is in this view's super view chain
 */
public func isSuper(v : UIView) -> Bool
{
    for var s : UIView? = self; s != nil; s = s?.superview {
        if(v == s) {
            return true;
        }
    }
    return false
}

public func constrain(attribute: NSLayoutAttribute, _ relation: NSLayoutRelation, _ otherView: UIView, _ otherAttribute: NSLayoutAttribute, constant: CGFloat = 0.0, multiplier : CGFloat = 1.0) -> UIView?
{
    let c = NSLayoutConstraint(item: self, attribute: attribute, relatedBy: relation, toItem: otherView, attribute: otherAttribute, multiplier: multiplier, constant: constant)

    if isSuper(otherView) {
        otherView.addConstraint(c)
        return self
    }
    else if(otherView.isSuper(self) || otherView == self)
    {
        self.addConstraint(c)
        return self
    }
    assert(false)
    return nil
}

public func constrain(attribute: NSLayoutAttribute, _ relation: NSLayoutRelation, constant: CGFloat, multiplier : CGFloat = 1.0) -> UIView?
{
    let c = NSLayoutConstraint(item: self, attribute: attribute, relatedBy: relation, toItem: nil, attribute: .NotAnAttribute, multiplier: multiplier, constant: constant)
    self.addConstraint(c)
    return self
}

}

当我尝试在调试器中调试视图的层次结构时,唯一具有白色背景的视图是tableView,但我已经清除了代码中的背景。我还尝试将tableView的backgroundView设置为nil,将backgroundView.backgroundColor设置为clearColor()。没有改变。

1 个答案:

答案 0 :(得分:0)

也许尝试将UITableView页脚设置为空白视图,并不是真的知道原因,但它会像您一样帮助解决类似的问题。

    [_tableView setTableFooterView:[[UIView alloc] init]];