tableView convertPoint: fromView: odd behaviour and workaround

前端 未结 2 586
时光说笑
时光说笑 2021-01-15 02:52

I have a tableView with custom cells. One of the objects in the cell is a textField. The tableViewController is the textFieldDelegate. The datasource for the table is an arr

2条回答
  •  感情败类
    2021-01-15 03:28

    -[UIView center] returns the center of the view's frame, which is in the view's superview's coordinate system. But -[UIView convertPoint:fromView:] expects to be given a point in the "from" view's coordinate system.

    Either give it the same point, and tell it to convert from the correct view:

    CGPoint point = [textField center];
    point = [self.tableView convertPoint:point fromView:textField.superview];
    

    Or give it a point in the view's coordinate system, and tell it to convert from the view:

    CGRect textFieldBounds = textField.bounds;
    CGPoint point = CGPointMake(CGRectGetMidX(textFieldBounds), CGRectGetMidY(textFieldBounds));
    point = [self.tableView convertPoint:point fromView:textField];
    

提交回复
热议问题