質問編集履歴

2

コードの追加

2018/08/14 09:18

投稿

Tommy716
Tommy716

スコア22

test CHANGED
File without changes
test CHANGED
@@ -11,3 +11,197 @@
11
11
 
12
12
 
13
13
  [参考サイト](https://i-app-tec.com/ios/textfield-scroll.html)
14
+
15
+
16
+
17
+ ```Swift
18
+
19
+ import UIKit
20
+
21
+
22
+
23
+ class ViewController: UIViewController {
24
+
25
+ @IBOutlet weak var scrollview: UIScrollView!
26
+
27
+ @IBOutlet weak var textView: UITextField!
28
+
29
+ @IBOutlet weak var label: UILabel!
30
+
31
+ @IBOutlet weak var orange: UIView!
32
+
33
+
34
+
35
+ var screenHeight:CGFloat!
36
+
37
+ // Screenの幅
38
+
39
+ var screenWidth:CGFloat!
40
+
41
+
42
+
43
+ override func viewDidLoad() {
44
+
45
+ super.viewDidLoad()
46
+
47
+
48
+
49
+ scrollview.delegate = (self as? UIScrollViewDelegate)
50
+
51
+ textView.delegate = self as? UITextFieldDelegate
52
+
53
+ // Do any additional setup after loading the view, typically from a nib.
54
+
55
+
56
+
57
+ let screenSize: CGRect = UIScreen.main.bounds
58
+
59
+ screenWidth = screenSize.width
60
+
61
+ screenHeight = screenSize.height
62
+
63
+ scrollview.frame.size =
64
+
65
+ CGSize(width: screenWidth, height: screenHeight)
66
+
67
+ scrollview.addSubview(orange)
68
+
69
+ scrollview.addSubview(label)
70
+
71
+ scrollview.addSubview(textView)
72
+
73
+ scrollview.contentSize = CGSize(width: screenWidth, height: screenHeight*2)
74
+
75
+ scrollview.bounces = false
76
+
77
+ print("screenWidth:(screenWidth)")
78
+
79
+ print("screenHeight:(screenHeight)")
80
+
81
+
82
+
83
+ }
84
+
85
+ func textFieldShouldReturn(_ textField: UITextField) -> Bool {
86
+
87
+ self.view.endEditing(true)
88
+
89
+ return true
90
+
91
+ }
92
+
93
+
94
+
95
+ override func viewWillAppear(_ animated: Bool) {
96
+
97
+ super.viewWillAppear(animated)
98
+
99
+
100
+
101
+ NotificationCenter.default.addObserver(self,
102
+
103
+ selector: #selector(ViewController.keyboardWillShow(_:)),
104
+
105
+ name: NSNotification.Name.UIKeyboardWillShow,
106
+
107
+ object: nil)
108
+
109
+ NotificationCenter.default.addObserver(self,
110
+
111
+ selector: #selector(ViewController.keyboardWillHide(_:)) ,
112
+
113
+ name: NSNotification.Name.UIKeyboardWillHide,
114
+
115
+ object: nil)
116
+
117
+ }
118
+
119
+
120
+
121
+ override func viewWillDisappear(_ animated: Bool) {
122
+
123
+ super.viewWillDisappear(animated)
124
+
125
+
126
+
127
+ NotificationCenter.default.removeObserver(self,
128
+
129
+ name: .UIKeyboardWillShow,
130
+
131
+ object: self.view.window)
132
+
133
+ NotificationCenter.default.removeObserver(self,
134
+
135
+ name: .UIKeyboardDidHide,
136
+
137
+ object: self.view.window)
138
+
139
+ }
140
+
141
+
142
+
143
+ @objc func keyboardWillShow(_ notification: Notification) {
144
+
145
+
146
+
147
+ let info = notification.userInfo!
148
+
149
+
150
+
151
+ let keyboardFrame = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
152
+
153
+
154
+
155
+ // bottom of textField
156
+
157
+ let bottomTextField = textView.frame.origin.y + textView.frame.height
158
+
159
+ // top of keyboard
160
+
161
+ let topKeyboard = screenHeight - keyboardFrame.size.height
162
+
163
+ // 重なり
164
+
165
+ let distance = bottomTextField - topKeyboard
166
+
167
+
168
+
169
+ if distance >= 0 {
170
+
171
+ // scrollViewのコンテツを上へオフセット + 20.0(追加のオフセット)
172
+
173
+ scrollview.contentOffset.y = distance + 20.0
174
+
175
+ }
176
+
177
+ }
178
+
179
+
180
+
181
+ @objc func keyboardWillHide(_ notification: Notification) {
182
+
183
+ scrollview.contentOffset.y = 0
184
+
185
+ }
186
+
187
+
188
+
189
+ override func didReceiveMemoryWarning() {
190
+
191
+ super.didReceiveMemoryWarning()
192
+
193
+ // Dispose of any resources that can be recreated.
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+ }
202
+
203
+
204
+
205
+
206
+
207
+ ```

1

文章の追加

2018/08/14 09:18

投稿

Tommy716
Tommy716

スコア22

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,8 @@
6
6
 
7
7
  具体的にしたいこととしては、text Fieldを編集するときにtext FIeldが隠れないようにしたいです。
8
8
 
9
+ コードは参考サイトと全く同じように書きました。
10
+
9
11
 
10
12
 
11
13
  [参考サイト](https://i-app-tec.com/ios/textfield-scroll.html)