こんにちは。
リンク内容
リンク先の通りに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}
回答1件
あなたの回答
tips
プレビュー