2016-12-24 11 views
-1

私はscrollViewにビューを追加しようとしましたが、手動でgreenViewに幅を設定するとうまくいきますが、constrantsを使用しようとすると、制約が機能しないようです。私は何か間違っていますか?誰も助けることができますか?ここでiOSのscrollViewで自動レイアウトをSwiftで使用するにはどうすればよいですか?

は私のコントローラとビューのコードです:

import UIKit 
import Material 

class UserProfileViewController: UIViewController { 

    override func loadView(){ 
     self.view = UserProfileView() 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

} 


import UIKit 
import Material 

class UserProfileView: UIView { 

    fileprivate var scrollView: UIScrollView! 

    convenience init(){ 
     self.init(frame: CGRect.zero) 
     self.backgroundColor = UIColor(red:0.15, green:0.24, blue:0.37, alpha:1.0) 
     prepareScrollView() 
     prepareMainView() 
    } 

    fileprivate func prepareScrollView() { 
     self.scrollView = UIScrollView(frame: UIScreen.main.bounds) 
     self.scrollView.contentSize = CGSize(width:self.scrollView.width, height: 1678) 
     self.addSubview(scrollView) 
    } 

    fileprivate func prepareMainView() { 
     let greenView = UIView() 
     greenView.backgroundColor = UIColor.green 
     greenView.height = 100 


     self.scrollView.addSubview(greenView) 

     self.scrollView.translatesAutoresizingMaskIntoConstraints = false 
     self.scrollView.isScrollEnabled = true 
     self.scrollView.isPagingEnabled = true 
     greenView.translatesAutoresizingMaskIntoConstraints = false 

     let views = [ 
      "scrollView": self.scrollView!, 
      "greenView": greenView, 
     ] as [String : Any] 



     let scrollViewHeight = NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView]|", metrics: nil, views: views) 
     let scrollViewWidth = NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView]|", metrics: nil, views: views) 
     let greenViewHorizontalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:|[scrollView][greenView][scrollView]|", metrics: nil, views: views) 
     let greenViewVerticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|[scrollView][greenView]|", metrics: nil, views: views) 

     self.addConstraints(scrollViewHeight) 
     self.addConstraints(scrollViewWidth) 
     self.addConstraints(greenViewHorizontalConstraint) 
     self.addConstraints(greenViewVerticalConstraint) 

    } 

} 
+0

これは制約の「古いスタイル」のコーディングです。ここで間違っている可能性があります。矛盾する制約の警告が表示されていますか?私はV/Hの2つのセットを読んでいます。どちらもビューとスクロールビューの両方を持っています。 – dfd

+0

いいえ、私は矛盾はありません。これを行う別の方法がありますか? –

+0

LayoutAnchors。それは基本的に異なる構文です。説明は非常に長く説明できます(それは答えではありません)ので、ここにブログ記事へのリンクhttp://useyourloaf.com/blog/pain-free-constraints-with-layout-anchors/があります。 Swift 2の構文ですが、Swift 3では何も変わっていません.Visual Forma Languageよりも簡単かどうかを判断できます。 – dfd

答えて

0

私はあなたが制約をアクティブ化コード内のどこにも見られません。

以下では、制約をどのようにアクティブにするかについて説明します。

//create 
let childredView = UIView() 
childredView.translatesAutoresizingMaskIntoConstraints = false 
childredView.backgroundColor = UIColor.purple 

//add it 
self.view.addSubview(childredView) 
self.view.bringSubview(toFront: childredView) 

//set constraints 
let views = ["childrenView": childredView] 
let vertical = NSLayoutConstraint.constraints(withVisualFormat: "V:|[childrenView]|", options: [], metrics: nil, views: views) 
let horizontal = NSLayoutConstraint.constraints(withVisualFormat: "H:|[childrenView]|", options: [], metrics: nil, views: views) 
let all = vertical + horizontal 

//activate them 
NSLayoutConstraint.activate(all) 

あなたのレイアウトアンカーを使用している場合は、あなたが作成した各1 YAnchorの終わりにisActive = trueを追加してください。例:

item.centerYAnchor.constraint(equalTo:self.view.centerYAnchor).isActive = true 
+0

ビューに制約を追加して、アクティブにします。 UIViewではうまく動作しますが、UIScrollViewではうまく動作しません。 –

+0

私はまた、活性化の方法を試しましたが、それでも同じ問題があります。 –

関連する問題