ストーリーボードでsafe area
にscrollView
を配置し、その中にstackView
を配置しています。
そして以下のコード(一部抜き出し)で、ストーリーボードで配置したaddButton
をタップされたらstackView
にtextField
とdeleteButton
が追加されるようにしました。
TextField
の編集が開始されたタイミングで、編集を開始したtextField
の座標を取得したいと考えています。
しかし、print(textField.frame)
で確認したところ、Y座標が0になってしまいます。
stackView
に対する座標を取得したいのですが、どのように書けば良いでしょうか?
=====
例えば、addButton
で追加された1つ目のtextFieldのY座標は32、2つ目のtextFieldのY座標は80のような数値を取得したいです。
(textFieldの高さが32、stackViewのスペースが16です。)
=====
import UIKit class ViewController: UIViewController, UIScrollViewDelegate { @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var stackView: UIStackView! var userPath:String! let fileManager = FileManager() var appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate override func viewDidLoad() { super.viewDidLoad() scrollView.delegate = self scrollView.bounces = false // TextFieldの処理 configureTextField() configureTapGesture() scrollView.addSubview(stackView) } override func viewDidAppear(_ animated: Bool) { let rect = stackView.frame scrollView.contentSize = CGSize(width: rect.size.width, height: rect.size.height + 112) } //---------------- @IBAction func addButton(_ sender: Any) { createTextField(id:Id ,text: "") } //---------------- //TextFieldを生成 func createTextField(id: Int, text: String) { let newView = StackViewCell() newView.heightAnchor.constraint(equalToConstant: 32).isActive = true newView.translatesAutoresizingMaskIntoConstraints = false newView.textField.delegate = self newView.textField.text = text stackView.addArrangedSubview(newView) let rect = stackView.frame scrollView.contentSize = CGSize(width: rect.size.width, height: rect.size.height + 112) } @objc func handleTap() { view.endEditing(true) print("handleTap") } private func configureTapGesture() { let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap)) view.addGestureRecognizer(tapGesture) } private func configureTextField() { } } extension ViewController: UITextFieldDelegate { func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { print("Start") print(scrollView.frame) // -> (58.0, 184.0, 298.0, 662.0) print(textField.frame) // -> (0.0, 1.0, 250.0, 30.0) return true } func textFieldShouldEndEditing(_ textField: UITextField) -> Bool { print("end") return true } } class StackViewCell: UIView { let textField = UITextField() let deleteButton = UIButton() init() { super.init(frame: CGRect()) // 削除ボタン deleteButton.setImage(UIImage(named: "マイナス(削除)ボタン"), for: .normal) deleteButton.addTarget(self, action: #selector(tapDeleteButton(_:)), for: .touchUpInside) addSubview(deleteButton) deleteButton.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true deleteButton.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true deleteButton.heightAnchor.constraint(equalTo: heightAnchor).isActive = true deleteButton.widthAnchor.constraint(equalTo: heightAnchor).isActive = true deleteButton.translatesAutoresizingMaskIntoConstraints = false // textField textField.clearButtonMode = UITextField.ViewMode.always textField.backgroundColor = UIColor.white textField.borderStyle = .roundedRect addSubview(textField) textField.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true textField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0.0).isActive = true textField.widthAnchor.constraint(equalTo: widthAnchor, constant: -48 ).isActive = true textField.translatesAutoresizingMaskIntoConstraints = false } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } @objc func tapDeleteButton(_ sender: UIButton) { // superview(StackView)から自身を削除 removeFromSuperview() } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/12/26 09:28 編集
2019/12/26 09:37
2019/12/26 10:53