Programmatically Add CenterX/CenterY Constraints

前端 未结 7 753

I have a UITableViewController that doesn\'t display any sections if there is nothing to show. I\'ve added a label to indicate to the user that there is nothing to display w

7条回答
  •  孤城傲影
    2020-11-29 18:57

    If you don't care about this question being specifically about a tableview, and you'd just like to center one view on top of another view here's to do it:

        let horizontalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: parentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        parentView.addConstraint(horizontalConstraint)
    
        let verticalConstraint = NSLayoutConstraint(item: newView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: parentView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        parentView.addConstraint(verticalConstraint)
    

提交回复
热议问题