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

質問編集履歴

2

SS追加

2015/10/21 05:37

投稿

Kesth
Kesth

スコア83

title CHANGED
File without changes
body CHANGED
@@ -199,16 +199,12 @@
199
199
  )
200
200
 
201
201
  ])
202
-
203
-
204
202
  }
205
-
206
-
207
203
 
204
+ }
208
205
 
209
206
 
207
+ ```
210
208
 
211
- }
212
209
 
213
-
214
- ```
210
+ ![イメージ説明](58b3f2987ffa67b6f6b1346ee2d48da9.png)

1

コード追加

2015/10/21 05:37

投稿

Kesth
Kesth

スコア83

title CHANGED
File without changes
body CHANGED
@@ -10,4 +10,205 @@
10
10
  文字をUITextView内でスクロールさせて表示させるという
11
11
  方法で実装したいです。
12
12
 
13
- 何卒ご教授のほどよろしくお願いします。
13
+ 何卒ご教授のほどよろしくお願いします。
14
+
15
+
16
+ ※追記
17
+
18
+ ソースコードを追記します。
19
+ 完成イメージとしては、SNSのコメント投稿画面のイメージです。
20
+
21
+
22
+ ```Swift
23
+ class ViewController:UIViewController,UITextViewDelegate,UITableViewDelegate, UITableViewDataSource{
24
+
25
+ var commentTableView = UITableView(frame: CGRectZero, style: .Grouped)
26
+ //コメント全体のContainer
27
+ var commentAreaContainer = UIView()
28
+ //入力欄
29
+ var commentInput = UITextView()
30
+
31
+
32
+ override func viewDidLoad() {
33
+ super.viewDidLoad()
34
+
35
+ commentTableView = UITableView()
36
+ commentTableView.translatesAutoresizingMaskIntoConstraints = false
37
+ self.view.addSubview(commentTableView)
38
+
39
+ commentAreaContainer.translatesAutoresizingMaskIntoConstraints = false
40
+ self.view.addSubview(commentAreaContainer)
41
+
42
+ commentInput.font = UIFont(name: "Helvetica Neue", size: 13.5)
43
+ commentInput.contentInset = UIEdgeInsetsMake(5,0,0,0)
44
+ commentInput.layoutManager.allowsNonContiguousLayout = false
45
+ commentInput.layer.cornerRadius = 2.0
46
+ commentInput.autoresizesSubviews = true
47
+ commentInput.translatesAutoresizingMaskIntoConstraints = false
48
+ commentInput.delegate = self
49
+ commentAreaContainer.addSubview(commentInput)
50
+
51
+ //Autolayoutを適用
52
+ setupConstaraints()
53
+ }
54
+
55
+ //input欄の高さが80まではinputの高さをtextに合わせて伸ばす
56
+ func textViewDidChange(textView: UITextView) {
57
+ let maxHeight: CGFloat = 80.0
58
+ if commentInput.frame.size.height < maxHeight {
59
+ let size: CGSize = commentInput.sizeThatFits(commentInput.frame.size)
60
+ //input欄の高さを合わせる
61
+ commentInput.frame.size.height = size.height
62
+ }
63
+
64
+
65
+ func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
66
+ textView.scrollRangeToVisible(textView.selectedRange)
67
+ return true
68
+ }
69
+
70
+ func setUpConstraints(){
71
+
72
+ self.view.addConstraints([
73
+
74
+ NSLayoutConstraint(
75
+ item: self.commentTableView,
76
+ attribute: .Top,
77
+ relatedBy: .Equal,
78
+ toItem: self.view,
79
+ attribute: .Top,
80
+ multiplier: 1.0,
81
+ constant: 0
82
+ ),
83
+
84
+ NSLayoutConstraint(
85
+ item: self.commentTableView,
86
+ attribute: .Right,
87
+ relatedBy: .Equal,
88
+ toItem: self.view,
89
+ attribute: .Right,
90
+ multiplier: 1.0,
91
+ constant: 0
92
+ ),
93
+
94
+ NSLayoutConstraint(
95
+ item: self.commentTableView,
96
+ attribute: .Left,
97
+ relatedBy: .Equal,
98
+ toItem: self.view,
99
+ attribute: .Left,
100
+ multiplier: 1.0,
101
+ constant: 0
102
+ ),
103
+
104
+ NSLayoutConstraint(
105
+ item: self.commentTableView,
106
+ attribute: .Bottom,
107
+ relatedBy: .Equal,
108
+ toItem: self.commentAreaContainer,
109
+ attribute: .Bottom,
110
+ multiplier: 1.0,
111
+ constant: -40
112
+ )
113
+ ])
114
+
115
+
116
+ self.view.addConstraints([
117
+
118
+ NSLayoutConstraint(
119
+ item: self.commentAreaContainer,
120
+ attribute: .Left,
121
+ relatedBy: .Equal,
122
+ toItem: self.view,
123
+ attribute: .Left,
124
+ multiplier: 1.0,
125
+ constant: 0
126
+ ),
127
+
128
+ NSLayoutConstraint(
129
+ item: self.commentAreaContainer,
130
+ attribute: .Right,
131
+ relatedBy: .Equal,
132
+ toItem: self.view,
133
+ attribute: .Right,
134
+ multiplier: 1.0,
135
+ constant: 0
136
+ ),
137
+
138
+ NSLayoutConstraint(
139
+ item: self.commentAreaContainer,
140
+ attribute: .Bottom,
141
+ relatedBy: .Equal,
142
+ toItem: self.view,
143
+ attribute: .Bottom,
144
+ multiplier: 1.0,
145
+ constant: 0
146
+ )
147
+ ])
148
+
149
+ self.view.addConstraints([
150
+
151
+ NSLayoutConstraint(
152
+ item: self.commentInput,
153
+ attribute: .Top,
154
+ relatedBy: .Equal,
155
+ toItem: self.commentAreaContainer,
156
+ attribute: .Top,
157
+ multiplier: 1.0,
158
+ constant: 5
159
+ ),
160
+
161
+ NSLayoutConstraint(
162
+ item: self.commentInput,
163
+ attribute: .Bottom,
164
+ relatedBy: .Equal,
165
+ toItem: self.commentAreaContainer,
166
+ attribute: .Bottom,
167
+ multiplier: 1.0,
168
+ constant: -5
169
+ ),
170
+
171
+ NSLayoutConstraint(
172
+ item: self.commentInput,
173
+ attribute: .Left,
174
+ relatedBy: .Equal,
175
+ toItem: self.commentAreaContainer,
176
+ attribute: .Left,
177
+ multiplier: 1.0,
178
+ constant: 6
179
+ ),
180
+
181
+ NSLayoutConstraint(
182
+ item: self.commentInput,
183
+ attribute: .Right,
184
+ relatedBy: .Equal,
185
+ toItem: self.commentAreaContainer,
186
+ attribute: .Right,
187
+ multiplier: 1.0,
188
+ constant: -6
189
+ ),
190
+
191
+ NSLayoutConstraint(
192
+ item: self.commentInput,
193
+ attribute: .Height,
194
+ relatedBy: .Equal,
195
+ toItem: nil,
196
+ attribute: .Height,
197
+ multiplier: 1.0,
198
+ constant: 32
199
+ )
200
+
201
+ ])
202
+
203
+
204
+ }
205
+
206
+
207
+
208
+
209
+
210
+
211
+ }
212
+
213
+
214
+ ```