teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

コードの追加

2018/08/14 09:18

投稿

Tommy716
Tommy716

スコア22

title CHANGED
File without changes
body CHANGED
@@ -4,4 +4,101 @@
4
4
  具体的にしたいこととしては、text Fieldを編集するときにtext FIeldが隠れないようにしたいです。
5
5
  コードは参考サイトと全く同じように書きました。
6
6
 
7
- [参考サイト](https://i-app-tec.com/ios/textfield-scroll.html)
7
+ [参考サイト](https://i-app-tec.com/ios/textfield-scroll.html)
8
+
9
+ ```Swift
10
+ import UIKit
11
+
12
+ class ViewController: UIViewController {
13
+ @IBOutlet weak var scrollview: UIScrollView!
14
+ @IBOutlet weak var textView: UITextField!
15
+ @IBOutlet weak var label: UILabel!
16
+ @IBOutlet weak var orange: UIView!
17
+
18
+ var screenHeight:CGFloat!
19
+ // Screenの幅
20
+ var screenWidth:CGFloat!
21
+
22
+ override func viewDidLoad() {
23
+ super.viewDidLoad()
24
+
25
+ scrollview.delegate = (self as? UIScrollViewDelegate)
26
+ textView.delegate = self as? UITextFieldDelegate
27
+ // Do any additional setup after loading the view, typically from a nib.
28
+
29
+ let screenSize: CGRect = UIScreen.main.bounds
30
+ screenWidth = screenSize.width
31
+ screenHeight = screenSize.height
32
+ scrollview.frame.size =
33
+ CGSize(width: screenWidth, height: screenHeight)
34
+ scrollview.addSubview(orange)
35
+ scrollview.addSubview(label)
36
+ scrollview.addSubview(textView)
37
+ scrollview.contentSize = CGSize(width: screenWidth, height: screenHeight*2)
38
+ scrollview.bounces = false
39
+ print("screenWidth:(screenWidth)")
40
+ print("screenHeight:(screenHeight)")
41
+
42
+ }
43
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
44
+ self.view.endEditing(true)
45
+ return true
46
+ }
47
+
48
+ override func viewWillAppear(_ animated: Bool) {
49
+ super.viewWillAppear(animated)
50
+
51
+ NotificationCenter.default.addObserver(self,
52
+ selector: #selector(ViewController.keyboardWillShow(_:)),
53
+ name: NSNotification.Name.UIKeyboardWillShow,
54
+ object: nil)
55
+ NotificationCenter.default.addObserver(self,
56
+ selector: #selector(ViewController.keyboardWillHide(_:)) ,
57
+ name: NSNotification.Name.UIKeyboardWillHide,
58
+ object: nil)
59
+ }
60
+
61
+ override func viewWillDisappear(_ animated: Bool) {
62
+ super.viewWillDisappear(animated)
63
+
64
+ NotificationCenter.default.removeObserver(self,
65
+ name: .UIKeyboardWillShow,
66
+ object: self.view.window)
67
+ NotificationCenter.default.removeObserver(self,
68
+ name: .UIKeyboardDidHide,
69
+ object: self.view.window)
70
+ }
71
+
72
+ @objc func keyboardWillShow(_ notification: Notification) {
73
+
74
+ let info = notification.userInfo!
75
+
76
+ let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
77
+
78
+ // bottom of textField
79
+ let bottomTextField = textView.frame.origin.y + textView.frame.height
80
+ // top of keyboard
81
+ let topKeyboard = screenHeight - keyboardFrame.size.height
82
+ // 重なり
83
+ let distance = bottomTextField - topKeyboard
84
+
85
+ if distance >= 0 {
86
+ // scrollViewのコンテツを上へオフセット + 20.0(追加のオフセット)
87
+ scrollview.contentOffset.y = distance + 20.0
88
+ }
89
+ }
90
+
91
+ @objc func keyboardWillHide(_ notification: Notification) {
92
+ scrollview.contentOffset.y = 0
93
+ }
94
+
95
+ override func didReceiveMemoryWarning() {
96
+ super.didReceiveMemoryWarning()
97
+ // Dispose of any resources that can be recreated.
98
+ }
99
+
100
+
101
+ }
102
+
103
+
104
+ ```

1

文章の追加

2018/08/14 09:18

投稿

Tommy716
Tommy716

スコア22

title CHANGED
File without changes
body CHANGED
@@ -2,5 +2,6 @@
2
2
  いつも通り、Scroll View内の部品に制約をつけるとビルドした時に真っ白になってしまします。何か方法はありますか?
3
3
 
4
4
  具体的にしたいこととしては、text Fieldを編集するときにtext FIeldが隠れないようにしたいです。
5
+ コードは参考サイトと全く同じように書きました。
5
6
 
6
7
  [参考サイト](https://i-app-tec.com/ios/textfield-scroll.html)