私はこの特定の質問に対するStackoverflowの回答のほとんどすべてを調べて、スクロールビューの使い方を教えてくれるチュートリアルを見ましたが、プロジェクトには適用されません。Swiftでプログラムで追加したときにUIScrollViewがスクロールしませんか?
ここに今まで私が知っていることです。スクロールビューが適切に機能するためには、最初にコンテンツサイズを与える必要があります。これによりスクロール可能な高さなどが決まります。
私はスクロールビューに上記の項目をどのように追加しているかを知るためにいくつかのコードを提供しています。私が間違っていることがある場合や、これを行うためのより良い方法がある場合は、私に知らせてください。私はまだSwiftとiOSの開発にかなり新しいです。私の考えでは、正しくやっているように感じます。
私は
- は、私は(入力フィールド、Imageviewsなど。)表示したい項目を作成して取っている段階
- 追加はのViewControllerのビューに項目を述べました。 (view.addsubview(等。))
- は
- 言ったにそれのすべての項目で私達のビューを追加scrollViewを作成し、画面/ビューと同じになるようにその制約を設定し、スクロールビュー
- リラックスして、すべては完璧な?????ここで
が私のコードですが、私はそれは長いかもしれない知っているが、私は私の質問の範囲は
class JobRegistrationController: UIViewController {
// ... Omitted for clarity
lazy var scrollView: UIScrollView = {
let view = UIScrollView(frame: UIScreen.main.bounds)
view.backgroundColor = .red
view.contentSize = CGSize(width: self.view.bounds.width, height: self.view.bounds.height * 2)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
//... Omitted for clarity
let scrollContentView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
// Need so that view controller is not behind nav controller
self.edgesForExtendedLayout = []
view.addSubview(scrollView)
scrollView.addSubview(scrollContentView)
scrollContentView.addSubview(jobTypeField)
scrollContentView.addSubview(jobTypeDividerLine)
// x, y, width and height constraints
scrollView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
// x, y, width and height constraints
scrollContentView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
scrollContentView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
scrollContentView.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
scrollContentView.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
// x, y, width and height constraints
jobTypeField.leftAnchor.constraint(equalTo: scrollContentView.leftAnchor, constant: 20).isActive = true
jobTypeField.topAnchor.constraint(equalTo: scrollContentView.topAnchor).isActive = true
jobTypeField.rightAnchor.constraint(equalTo: scrollContentView.rightAnchor, constant: -8).isActive = true
jobTypeField.heightAnchor.constraint(equalToConstant: 30).isActive = true
// x, y, width and height constraints
jobTypeDividerLine.leftAnchor.constraint(equalTo: scrollContentView.leftAnchor, constant: 20).isActive = true
jobTypeDividerLine.topAnchor.constraint(equalTo: jobTypeField.bottomAnchor).isActive = true
jobTypeDividerLine.rightAnchor.constraint(equalTo: scrollContentView.rightAnchor).isActive = true
jobTypeDividerLine.heightAnchor.constraint(equalToConstant: 0.5).isActive = true
こんにちは、提案に感謝します。私のビューでスクロールすると、上下に動くスクロールバーが見えますが、私のビューの内容は動いていません –
@EdwardLimそれは別の問題です。解決策は、すべてのサブビューを1つのUIViewに入れて、単一のUIViewをscrollview内のサブビューとして追加することです。単一のUIView "scrollContentView"をすべて持たずに、すべてのサブビューをscrollView内に置かないようにします。さもなければ、例えばあなたが説明したように大規模なバグに遭遇します。 –
こんにちは、これを正しく行う方法をお勧めしますか?私はすでにスクロールビューにビューを追加しようとしましたが、まったく同じことをしているようです。 –