2017-10-18 15 views
0

UIScrollViewには、UIImageViewUILabelが設定されています。 しかし、それは私が望むようにスクロールしていません。それはそこにもないようです...どうすればこの問題を解決できますか?私はちょうどそれがスクロール可能であることを望む(まだスクロールするために底に何もまだスクロールすることがない)UIScrollViewはスクロールできませんか?

Thxは事前に!

// scrollView 
    scrollView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(scrollView) 

    // scrollView constraints 
    scrollView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true 
    scrollView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true 
    scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true 
    scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true 

    // Initialize profileImageView 
    profileImageView.image = #imageLiteral(resourceName: "neil") 
    profileImageView.translatesAutoresizingMaskIntoConstraints = false 
    profileImageView.layer.cornerRadius = 125/2 
    profileImageView.clipsToBounds = true 
    scrollView.addSubview(profileImageView) 

    // Add profileImageView constraints 
    profileImageView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 45).isActive = true 
    profileImageView.heightAnchor.constraint(equalToConstant: 125).isActive = true 
    profileImageView.widthAnchor.constraint(equalToConstant: 125).isActive = true 
    profileImageView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true 

    // Add separator view 
    seperator.translatesAutoresizingMaskIntoConstraints = false 
    seperator.backgroundColor = UIColor.darkGray 
    scrollView.addSubview(seperator) 

    // seperator constraints 
    seperator.heightAnchor.constraint(equalToConstant: 2).isActive = true 
    seperator.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 0.8).isActive = true 
    seperator.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true 
    seperator.topAnchor.constraint(equalTo: profileImageView.bottomAnchor, constant: 20).isActive = true 

    //nameLabel 
    let nameLabelFont = UIFont.monospacedDigitSystemFont(ofSize: 36, weight: UIFont.Weight.heavy) 
    nameLabel.font = nameLabelFont 
    nameLabel.text = currentUser.name 
    nameLabel.translatesAutoresizingMaskIntoConstraints = false 
    scrollView.addSubview(nameLabel) 

    // nameLabel constraints 
    nameLabel.topAnchor.constraint(equalTo: seperator.bottomAnchor, constant: 10).isActive = true 
    nameLabel.leadingAnchor.constraint(equalTo: seperator.leadingAnchor).isActive = true 

答えて

1

scrollView.contentSizeがあなたの視野そして大きくないので、あなたがスクロールすることはできません。

は、ここに私のコードです。

画面をスクロール可能にする場合は、scrollView.contentSizeを設定する必要があります。

view.addSubview(scrollView)の前にscrollView.contentSize = CGSize(width: 1000, height: 1000)を置き、スクロール可能な領域を1000x1000に設定します。

一般にscrollView.contentSizeは、UIScrollViewの内部のUI要素に基づいて計算する必要があります。スペースを挟んで10枚の画像のサイズ。

+0

ありがとう、そんなに助けてくれました! –

関連する問題