在表格视图单元格中查找UIView的x位置

时间:2016-12-08 06:12:11

标签: ios swift uitableview uiview

我想在UIView的中心放置一条线。

the line is not at the centre of my icon

override func draw(_ rect: CGRect) {
    if let anchor =  anchorView(){
        print("Anchor x origin : \(anchor.bounds.origin.x)")
        anchor.backgroundColor = UIColor.blue
        timeline.start = CGPoint(x: anchor.bounds.origin.x + anchor.frame.width/2 , y: 0)
        timeline.middle = CGPoint(x: timeline.start.x, y: anchor.frame.origin.y)
        timeline.end = CGPoint(x: timeline.start.x, y: self.bounds.size.height)
        timeline.draw(view: self.contentView)
    }else{
        print("this should not happen")
    }
}

我得到UIView的x位置等于零

1 个答案:

答案 0 :(得分:1)

UIView的{​​{1}}可能就是您所需要的:

func convert(_ point: CGPoint, to view: UIView?) -> CGPoint

然后您可以使用let centerRelativeToTableView = anchor.superview!.convert(anchor.center, to: tableView)