2016-12-08 128 views
0

我想在UIView的中心放置一條直線。在表格視圖單元格中查找UIView的x位置

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位置等於零

+0

你可以設置x po sview of line like yourview.frame.size.origin.x + yourview.frame.size.width/2 –

+0

我得到這個錯誤:「CGSize類型的值沒有成員'起源' –

回答

1

UIViewfunc convert(_ point: CGPoint, to view: UIView?) -> CGPoint可能是你需要的東西:

let centerRelativeToTableView = anchor.superview!.convert(anchor.center, to: tableView) 

然後你可以使用centerRelativeToTableView.x

+0

謝謝..它的工作... :) –

相關問題