質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2450閲覧

StackView内の部品の座標が(0,0)になる。

tafuzz

総合スコア15

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/12/26 07:12

ストーリーボードでsafe areascrollViewを配置し、その中にstackViewを配置しています。
そして以下のコード(一部抜き出し)で、ストーリーボードで配置したaddButtonをタップされたらstackViewtextFielddeleteButtonが追加されるようにしました。

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() } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

convertを利用すると、指定したUIView内でのframeを取得できます。
こちらはいかがでしょうか。

投稿2019/12/26 07:43

編集2019/12/26 09:38
MasatoUchida

総合スコア134

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tafuzz

2019/12/26 09:28 編集

回答ありがとうございます。 いまいち記述方法が分からないのですが、今回の場合、 let convertFrame = self.view.convert(textField.frame.origin, from: stackView) でいいのでしょうか? print(convertFrame)の結果、どのTextFieldでも(58.0, 185.0)になってしまいます。
MasatoUchida

2019/12/26 09:37

私が貼ったURLが間違っていました。失礼いたしました。デバッグしていませんが、おそらくfromではなくtoだと思います。
tafuzz

2019/12/26 10:53

ありがとうございます。 convrtFrame = textField.convert(textField.frame, to: self.view) とすることで解決できました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問