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

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

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

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

Swift

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

Q&A

解決済

1回答

1100閲覧

scrollview とsignalSIGABRT

midori0822

総合スコア61

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/04/12 09:33

編集2018/04/13 06:00

こんにちは。

リンク内容
リンク先の通りにscrollviewを使用したいと思います。
リンク先ではtextfieldが編集される時に、textfieldのみを移動させてます。僕はkeyboard上へbuttonとtextfieldを同時に移動させるため、viewにその2つを配置し、そのviewを平行移動させようとしました。
(let txtLimit = txtActiveField.frame.origin.y + txtActiveField.frame.height + 8.0 のtxttxtActiveField→movingBox(2つの部品を配置したview) )
こうするとビルドはうまく行ったのですが、いざtextfieldを編集しようとするとクラッシュします。
これの原因はなんだと思いますか?

2つの部品の制約はmovingBoxのみに依存するよう設定してあります

storyboard上では接続による警告はないようです。
考えられるところの補足情報が必要であればなんでも聞いてください。

回答お待ちしております。

追記:navigation bar 分の高さを計算に入れてなかったのですがそれが要因になり得ますか?
試してみたのですが解決できませんでした

>newmt様への修正依頼

swift

1import UIKit 2 3class ViewController: UIViewController, UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate{ 4 5 let userDefaults = UserDefaults.standard 6 7 @IBOutlet weak var movingBox: UIView! 8 @IBOutlet weak var scvBackGround: UIScrollView! 9 10 11 @IBOutlet weak var memoText: UITextField! 12 13 override func viewWillAppear(_ animated: Bool) { 14 super.viewWillAppear(animated) 15 16 memoText.delegate = self 17 18 let notificationCenter = NotificationCenter.default 19 notificationCenter.addObserver(self, selector: Selector(("handleKeyboardWillShowNotification:")), name: NSNotification.Name.UIKeyboardWillShow, object: nil) 20 notificationCenter.addObserver(self, selector: Selector(("handleKeyboardWillHideNotification:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 21 } 22 override func viewWillDisappear(_ animated: Bool) { 23 let notificationCenter = NotificationCenter.default 24 notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) 25 notificationCenter.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) 26 } 27 28 override func viewDidLoad() { 29 super.viewDidLoad() 30 31 memoText.returnKeyType = .done 32 33 } 34 35 override func didReceiveMemoryWarning() { 36 super.didReceiveMemoryWarning() 37 // Dispose of any resources that can be recreated. 38 } 39 40 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 41 memoText.resignFirstResponder() 42 return true 43 } 44 45 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 46 selectArray.removeAll() 47 for _ in memoArray { 48 selectArray.append(false) 49 } 50 self.view.endEditing(true) 51 } 52 53 func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { 54 55 return true 56 } 57 func handleKeyboardWillShowNotification(notification: NSNotification){ 58 let userInfo = notification.userInfo! 59 let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue 60 let myBoundSize: CGSize = UIScreen.main.bounds.size 61 let navBarHeight:CGFloat = self.navigationController!.navigationBar.bounds.size.height 62 63 64 let movingBottom = movingBox.frame.origin.y + movingBox.frame.height + navBarHeight 65 let keyboardTop = myBoundSize.height - navBarHeight - keyboardScreenEndFrame.size.height 66 67 if movingBottom >= keyboardTop { 68 scvBackGround.contentOffset.y = movingBottom - keyboardTop 69 } 70 } 71 func handleKeyboardWillHideNotification(notification: NSNotification) { 72 scvBackGround.contentOffset.y = 0 73 } 74 75}

階層構造
イメージ説明
movingBox
イメージ説明
movingBoxとその上の2つの部品のconstrain
イメージ説明

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

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

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

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

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

newmt

2018/04/12 22:17 編集

storyboardのスクリーンショットやコード内でUITextFieldの設定をしている部分などを見せていただくことはできませんでしょうか?
midori0822

2018/04/13 06:01

質問文修正しました!
newmt

2018/04/13 11:18

3点お伺いさせてください。1. リンク先の「6 テキストフィールドのデリゲート追加」は実施していますでしょうか? 2. エラーとなっている箇所は特定できますでしょうか?どこか特定の処理に入った後にエラーが発生していますでしょうか? 3. selectArrayとmemoArrayはどこで宣言されていらっしゃいますでしょうか?
midori0822

2018/04/13 12:01

お手数おかけして申し訳ございません…ありがとうございます。 1点目、上記の通りviewWillApp()で実施しています。2点目、 handleKeyboardWillShowNotification()で処理が止まっています。三点目はどちらもなにもないところで宣言しています。memoTextの接続下あたりです。
guest

回答1

0

ベストアンサー

Notificationに関する記載を下記のように変更したらいかがでしょうか?Swiftのバージョンによって記載が変わっていますので、そこが関係しているのかと思います。

let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: Selector(("handleKeyboardWillShowNotification:")), name: NSNotification.Name.UIKeyboardWillShow, object: nil) notificationCenter.addObserver(self, selector: Selector(("handleKeyboardWillHideNotification:")), name: NSNotification.Name.UIKeyboardWillHide, object: nil) ↓ let notificationCenter = NotificationCenter.default notificationCenter.addObserver(self, selector: #selector(handleKeyboardWillShowNotification), name: .UIKeyboardWillShow, object: nil) notificationCenter.addObserver(self, selector: #selector(handleKeyboardWillHideNotification), name: .UIKeyboardWillHide, object: nil)
func handleKeyboardWillShowNotification(notification: NSNotification){ } func handleKeyboardWillHideNotification(notification: NSNotification) { } ↓ @objc func handleKeyboardWillShowNotification(notification: NSNotification){ } @objc func handleKeyboardWillHideNotification(notification: NSNotification) { }

投稿2018/04/13 21:48

編集2018/04/13 21:54
newmt

総合スコア1277

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

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

midori0822

2018/04/14 08:39

無事解決しました!!newmt様長い間お付き合い頂きありがとうございました! ずっと試行錯誤していたので本当に感謝します! scvBackGround.contentOffset.y = movingBottom - keyboardTop → scvBackGround.contentOffset.y = myBoundSize.height - keyboardTop にすることで位置もうまく調整できました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問