How can I get UITextField origin y position relative to UIScreen?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How can I get UITextField origin y position relative to UIScreen?



I have many UITextFields in UITableViewCell(xib).


UITextFields


UITableViewCell(xib)



UITableView is also has many UITableViewCell(xib) all different styles.


UITableView


UITableViewCell(xib)


class CustomTableViewCell: UITableViewCell

@IBOutlet weak var fieldOne: UITextField!
@IBOutlet weak var fieldTwo: UITextField!
@IBOutlet weak var fieldThree: UITextField!
@IBOutlet weak var fieldFour: UITextField!
.
.
.
@IBOutlet weak var fieldTwelve: UITextField!

func textFieldDidBeginEditing(_ textField: UITextField)
//how to get Origin Y position relative to UIScreen or UITableView/UIViewController?






How can I get UITextField origin y position relative to UIScreen or UITableView/UIViewController in UITableViewCell swift?


UITextField


UIScreen


UITableView/UIViewController


UITableViewCell





can you share the screenshot?
– Priyal
Aug 12 at 5:24




2 Answers
2



You can use the func convert(_ point: CGPoint, to view: UIView?) -> CGPoint method of UIView. UITextField is of course a subclass for UIView.


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


UITextField


UIView



To convert the origin point (0, 0) relative to the window, pass nil:


textField.convert(CGPoint.zero, to: nil)



Relative to TableView:


textField.convert(CGPoint.zero, to: tableView)



You can add a method in your cell. You can call this method in your table view and passing the table view as a parameter.


func convertTextFieldOriginToView(_ view: UIView) -> CGPoint
return textField.convert(CGPoint.zero, to: view)





Thanks But I can't access tableView in CustomTableViewCell.swift without using the delegate. I want to calculate origin y without using the delegate.
– Shawn Baek
Aug 12 at 5:42






@ShawnBaek I edited my answer. Will it help?
– ukim
Aug 12 at 6:10



Try the below code to get relative position wrt UITableView.


UITableView


let pointInTable = textField.superview?.convert(textField.frame.origin, to: self.table)



Hope this will help.





Thank you for the reply. Is there a way to access tableView in CustomTableViewCell.swift without delegate? How to access self.table in CustomTableViewCell.swift?
– Shawn Baek
Aug 12 at 5:44






you can try in the CustomTableViewCell as self.superview !
– vivekDas
Aug 12 at 5:49






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered