Use Xib with Scroll View - Swift












0















I try to use Xib files with UIScrollView. I set the constraints (top ,bottom ,leading ,trailing . equal width and height (priority 250)).



I added the xibs to view that inside of UIScrollView. And set the last element bottom anchor with greaterThanOrEqualTo .



But scrollview not scroll . Where is the problem ?



Thanks in advance.



@IBOutlet weak var scrollView: UIScrollView!
@IBOutlet weak var scrollViewSubView: UIView!

var campaignCollectionView:UICollectionView!
var pageControl:UIPageControl!
var winGiftVoucherCode:WinGiftVoucherView!
var lastCallDiscountView : LastCallDiscountView!

var lastCallDiscountView2 : LastCallDiscountView!
var lastCallDiscountView3 : LastCallDiscountView!

override func viewDidLoad() {
super.viewDidLoad()

setupNavigationBarItems()
setCollectionView()
setPageControl()
winGiftVoucherCodeFunc()
lastCallDiscountFunc()

lastCallDiscountFunc2()
lastCallDiscountFunc3()

}



func setCollectionView(){

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
let cCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
campaignCollectionView = cCV
campaignCollectionView.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewSubView.addSubview(campaignCollectionView)

campaignCollectionView.dataSource = self
campaignCollectionView.delegate = self
campaignCollectionView.backgroundColor = .white
campaignCollectionView.isPagingEnabled = true
campaignCollectionView.isScrollEnabled = true
campaignCollectionView.showsHorizontalScrollIndicator = false

if #available(iOS 11.0, *) {
campaignCollectionView.topAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
} else {
campaignCollectionView.topAnchor.constraint(equalTo: self.scrollViewSubView.topAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
}

if #available(iOS 11.0, *) {
campaignCollectionView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
} else {
campaignCollectionView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
campaignCollectionView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
} else {
campaignCollectionView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
}

campaignCollectionView.register(UINib(nibName: "CampaignCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CampaignCollectionViewCell")

}


func setPageControl(){
let pC = UIPageControl()
pageControl = pC
pageControl.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewSubView.addSubview(pageControl)

if #available(iOS 11.0, *) {
pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
} else {
pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
}

if #available(iOS 11.0, *) {
pageControl.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
} else {
pageControl.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
pageControl.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
} else {
pageControl.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
}

//pageControl.backgroundColor = .black
//pageControl.tintColor = .white
pageControl.pageIndicatorTintColor = .lightGray
pageControl.currentPageIndicatorTintColor = .black

pageControl.numberOfPages = 3
pageControl.currentPage = 0
//pageControl.hidesForSinglePage = true
}

func winGiftVoucherCodeFunc(){

let wGVC : WinGiftVoucherView = UIView.fromNib()
winGiftVoucherCode = wGVC
winGiftVoucherCode.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewSubView.addSubview(winGiftVoucherCode)

if #available(iOS 11.0, *) {
winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
} else {
winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
}

if #available(iOS 11.0, *) {
winGiftVoucherCode.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
} else {
winGiftVoucherCode.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
winGiftVoucherCode.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
} else {
winGiftVoucherCode.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
}

let rate : CGFloat = 0.85

let mainRect = winGiftVoucherCode.frame
let rec = winGiftVoucherCode.progressX.frame
let aa = ProgressView(frame: CGRect(x: 0, y: 0, width: ( mainRect.size.width - 100 ) * rate , height: rec.size.height) )
aa.backgroundColor = UIColor.black
self.winGiftVoucherCode.progressX.addSubview(aa)

winGiftVoucherCode.voucherRemainderViewLeftConstraint.constant = ( winGiftVoucherCode.frame.width - 100 ) * rate - 20


}

func lastCallDiscountFunc(){

let lCDV : LastCallDiscountView = UIView.fromNib()
lastCallDiscountView = lCDV
lastCallDiscountView.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewSubView.addSubview(lastCallDiscountView)

if #available(iOS 11.0, *) {
lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
} else {
lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
} else {
lastCallDiscountView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
} else {
lastCallDiscountView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
}

}

func lastCallDiscountFunc2(){

let lCDV : LastCallDiscountView = UIView.fromNib()
lastCallDiscountView2 = lCDV
lastCallDiscountView2.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewSubView.addSubview(lastCallDiscountView2)

if #available(iOS 11.0, *) {
lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
} else {
lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
} else {
lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
// campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView2.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
} else {
lastCallDiscountView2.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView2.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
} else {
lastCallDiscountView2.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
}

}

func lastCallDiscountFunc3(){

let lCDV : LastCallDiscountView = UIView.fromNib()
lastCallDiscountView3 = lCDV
lastCallDiscountView3.translatesAutoresizingMaskIntoConstraints = false
self.scrollViewSubView.addSubview(lastCallDiscountView3)

if #available(iOS 11.0, *) {
lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
} else {
lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
} else {
lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView3.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
} else {
lastCallDiscountView3.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView3.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
} else {
lastCallDiscountView3.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
}

if #available(iOS 11.0, *) {
lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.safeAreaLayoutGuide.bottomAnchor, constant: 10).isActive = true
} else {
lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.bottomAnchor, constant: 10).isActive = true
}

}









share|improve this question



























    0















    I try to use Xib files with UIScrollView. I set the constraints (top ,bottom ,leading ,trailing . equal width and height (priority 250)).



    I added the xibs to view that inside of UIScrollView. And set the last element bottom anchor with greaterThanOrEqualTo .



    But scrollview not scroll . Where is the problem ?



    Thanks in advance.



    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var scrollViewSubView: UIView!

    var campaignCollectionView:UICollectionView!
    var pageControl:UIPageControl!
    var winGiftVoucherCode:WinGiftVoucherView!
    var lastCallDiscountView : LastCallDiscountView!

    var lastCallDiscountView2 : LastCallDiscountView!
    var lastCallDiscountView3 : LastCallDiscountView!

    override func viewDidLoad() {
    super.viewDidLoad()

    setupNavigationBarItems()
    setCollectionView()
    setPageControl()
    winGiftVoucherCodeFunc()
    lastCallDiscountFunc()

    lastCallDiscountFunc2()
    lastCallDiscountFunc3()

    }



    func setCollectionView(){

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    let cCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
    campaignCollectionView = cCV
    campaignCollectionView.translatesAutoresizingMaskIntoConstraints = false
    self.scrollViewSubView.addSubview(campaignCollectionView)

    campaignCollectionView.dataSource = self
    campaignCollectionView.delegate = self
    campaignCollectionView.backgroundColor = .white
    campaignCollectionView.isPagingEnabled = true
    campaignCollectionView.isScrollEnabled = true
    campaignCollectionView.showsHorizontalScrollIndicator = false

    if #available(iOS 11.0, *) {
    campaignCollectionView.topAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
    } else {
    campaignCollectionView.topAnchor.constraint(equalTo: self.scrollViewSubView.topAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    } else {
    campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
    }

    if #available(iOS 11.0, *) {
    campaignCollectionView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    } else {
    campaignCollectionView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    campaignCollectionView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    } else {
    campaignCollectionView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
    }

    campaignCollectionView.register(UINib(nibName: "CampaignCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CampaignCollectionViewCell")

    }


    func setPageControl(){
    let pC = UIPageControl()
    pageControl = pC
    pageControl.translatesAutoresizingMaskIntoConstraints = false
    self.scrollViewSubView.addSubview(pageControl)

    if #available(iOS 11.0, *) {
    pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
    } else {
    pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    } else {
    pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
    }

    if #available(iOS 11.0, *) {
    pageControl.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    } else {
    pageControl.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    pageControl.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    } else {
    pageControl.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
    }

    //pageControl.backgroundColor = .black
    //pageControl.tintColor = .white
    pageControl.pageIndicatorTintColor = .lightGray
    pageControl.currentPageIndicatorTintColor = .black

    pageControl.numberOfPages = 3
    pageControl.currentPage = 0
    //pageControl.hidesForSinglePage = true
    }

    func winGiftVoucherCodeFunc(){

    let wGVC : WinGiftVoucherView = UIView.fromNib()
    winGiftVoucherCode = wGVC
    winGiftVoucherCode.translatesAutoresizingMaskIntoConstraints = false
    self.scrollViewSubView.addSubview(winGiftVoucherCode)

    if #available(iOS 11.0, *) {
    winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
    } else {
    winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    } else {
    winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
    }

    if #available(iOS 11.0, *) {
    winGiftVoucherCode.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    } else {
    winGiftVoucherCode.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    winGiftVoucherCode.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    } else {
    winGiftVoucherCode.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
    }

    let rate : CGFloat = 0.85

    let mainRect = winGiftVoucherCode.frame
    let rec = winGiftVoucherCode.progressX.frame
    let aa = ProgressView(frame: CGRect(x: 0, y: 0, width: ( mainRect.size.width - 100 ) * rate , height: rec.size.height) )
    aa.backgroundColor = UIColor.black
    self.winGiftVoucherCode.progressX.addSubview(aa)

    winGiftVoucherCode.voucherRemainderViewLeftConstraint.constant = ( winGiftVoucherCode.frame.width - 100 ) * rate - 20


    }

    func lastCallDiscountFunc(){

    let lCDV : LastCallDiscountView = UIView.fromNib()
    lastCallDiscountView = lCDV
    lastCallDiscountView.translatesAutoresizingMaskIntoConstraints = false
    self.scrollViewSubView.addSubview(lastCallDiscountView)

    if #available(iOS 11.0, *) {
    lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
    } else {
    lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    } else {
    lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    } else {
    lastCallDiscountView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    } else {
    lastCallDiscountView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
    }

    }

    func lastCallDiscountFunc2(){

    let lCDV : LastCallDiscountView = UIView.fromNib()
    lastCallDiscountView2 = lCDV
    lastCallDiscountView2.translatesAutoresizingMaskIntoConstraints = false
    self.scrollViewSubView.addSubview(lastCallDiscountView2)

    if #available(iOS 11.0, *) {
    lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
    } else {
    lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
    } else {
    lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
    // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView2.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    } else {
    lastCallDiscountView2.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView2.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    } else {
    lastCallDiscountView2.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
    }

    }

    func lastCallDiscountFunc3(){

    let lCDV : LastCallDiscountView = UIView.fromNib()
    lastCallDiscountView3 = lCDV
    lastCallDiscountView3.translatesAutoresizingMaskIntoConstraints = false
    self.scrollViewSubView.addSubview(lastCallDiscountView3)

    if #available(iOS 11.0, *) {
    lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
    } else {
    lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
    } else {
    lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView3.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
    } else {
    lastCallDiscountView3.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView3.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
    } else {
    lastCallDiscountView3.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
    }

    if #available(iOS 11.0, *) {
    lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.safeAreaLayoutGuide.bottomAnchor, constant: 10).isActive = true
    } else {
    lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.bottomAnchor, constant: 10).isActive = true
    }

    }









    share|improve this question

























      0












      0








      0








      I try to use Xib files with UIScrollView. I set the constraints (top ,bottom ,leading ,trailing . equal width and height (priority 250)).



      I added the xibs to view that inside of UIScrollView. And set the last element bottom anchor with greaterThanOrEqualTo .



      But scrollview not scroll . Where is the problem ?



      Thanks in advance.



      @IBOutlet weak var scrollView: UIScrollView!
      @IBOutlet weak var scrollViewSubView: UIView!

      var campaignCollectionView:UICollectionView!
      var pageControl:UIPageControl!
      var winGiftVoucherCode:WinGiftVoucherView!
      var lastCallDiscountView : LastCallDiscountView!

      var lastCallDiscountView2 : LastCallDiscountView!
      var lastCallDiscountView3 : LastCallDiscountView!

      override func viewDidLoad() {
      super.viewDidLoad()

      setupNavigationBarItems()
      setCollectionView()
      setPageControl()
      winGiftVoucherCodeFunc()
      lastCallDiscountFunc()

      lastCallDiscountFunc2()
      lastCallDiscountFunc3()

      }



      func setCollectionView(){

      let layout = UICollectionViewFlowLayout()
      layout.scrollDirection = .horizontal
      let cCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
      campaignCollectionView = cCV
      campaignCollectionView.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(campaignCollectionView)

      campaignCollectionView.dataSource = self
      campaignCollectionView.delegate = self
      campaignCollectionView.backgroundColor = .white
      campaignCollectionView.isPagingEnabled = true
      campaignCollectionView.isScrollEnabled = true
      campaignCollectionView.showsHorizontalScrollIndicator = false

      if #available(iOS 11.0, *) {
      campaignCollectionView.topAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
      } else {
      campaignCollectionView.topAnchor.constraint(equalTo: self.scrollViewSubView.topAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      campaignCollectionView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      campaignCollectionView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      campaignCollectionView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      campaignCollectionView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      campaignCollectionView.register(UINib(nibName: "CampaignCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CampaignCollectionViewCell")

      }


      func setPageControl(){
      let pC = UIPageControl()
      pageControl = pC
      pageControl.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(pageControl)

      if #available(iOS 11.0, *) {
      pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
      } else {
      pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      pageControl.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      pageControl.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      pageControl.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      pageControl.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      //pageControl.backgroundColor = .black
      //pageControl.tintColor = .white
      pageControl.pageIndicatorTintColor = .lightGray
      pageControl.currentPageIndicatorTintColor = .black

      pageControl.numberOfPages = 3
      pageControl.currentPage = 0
      //pageControl.hidesForSinglePage = true
      }

      func winGiftVoucherCodeFunc(){

      let wGVC : WinGiftVoucherView = UIView.fromNib()
      winGiftVoucherCode = wGVC
      winGiftVoucherCode.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(winGiftVoucherCode)

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
      } else {
      winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      winGiftVoucherCode.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      winGiftVoucherCode.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      let rate : CGFloat = 0.85

      let mainRect = winGiftVoucherCode.frame
      let rec = winGiftVoucherCode.progressX.frame
      let aa = ProgressView(frame: CGRect(x: 0, y: 0, width: ( mainRect.size.width - 100 ) * rate , height: rec.size.height) )
      aa.backgroundColor = UIColor.black
      self.winGiftVoucherCode.progressX.addSubview(aa)

      winGiftVoucherCode.voucherRemainderViewLeftConstraint.constant = ( winGiftVoucherCode.frame.width - 100 ) * rate - 20


      }

      func lastCallDiscountFunc(){

      let lCDV : LastCallDiscountView = UIView.fromNib()
      lastCallDiscountView = lCDV
      lastCallDiscountView.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(lastCallDiscountView)

      if #available(iOS 11.0, *) {
      lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
      } else {
      lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      }

      func lastCallDiscountFunc2(){

      let lCDV : LastCallDiscountView = UIView.fromNib()
      lastCallDiscountView2 = lCDV
      lastCallDiscountView2.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(lastCallDiscountView2)

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
      } else {
      lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView2.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView2.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      }

      func lastCallDiscountFunc3(){

      let lCDV : LastCallDiscountView = UIView.fromNib()
      lastCallDiscountView3 = lCDV
      lastCallDiscountView3.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(lastCallDiscountView3)

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
      } else {
      lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
      } else {
      lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView3.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView3.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.safeAreaLayoutGuide.bottomAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.bottomAnchor, constant: 10).isActive = true
      }

      }









      share|improve this question














      I try to use Xib files with UIScrollView. I set the constraints (top ,bottom ,leading ,trailing . equal width and height (priority 250)).



      I added the xibs to view that inside of UIScrollView. And set the last element bottom anchor with greaterThanOrEqualTo .



      But scrollview not scroll . Where is the problem ?



      Thanks in advance.



      @IBOutlet weak var scrollView: UIScrollView!
      @IBOutlet weak var scrollViewSubView: UIView!

      var campaignCollectionView:UICollectionView!
      var pageControl:UIPageControl!
      var winGiftVoucherCode:WinGiftVoucherView!
      var lastCallDiscountView : LastCallDiscountView!

      var lastCallDiscountView2 : LastCallDiscountView!
      var lastCallDiscountView3 : LastCallDiscountView!

      override func viewDidLoad() {
      super.viewDidLoad()

      setupNavigationBarItems()
      setCollectionView()
      setPageControl()
      winGiftVoucherCodeFunc()
      lastCallDiscountFunc()

      lastCallDiscountFunc2()
      lastCallDiscountFunc3()

      }



      func setCollectionView(){

      let layout = UICollectionViewFlowLayout()
      layout.scrollDirection = .horizontal
      let cCV = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
      campaignCollectionView = cCV
      campaignCollectionView.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(campaignCollectionView)

      campaignCollectionView.dataSource = self
      campaignCollectionView.delegate = self
      campaignCollectionView.backgroundColor = .white
      campaignCollectionView.isPagingEnabled = true
      campaignCollectionView.isScrollEnabled = true
      campaignCollectionView.showsHorizontalScrollIndicator = false

      if #available(iOS 11.0, *) {
      campaignCollectionView.topAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.topAnchor, constant: 10).isActive = true
      } else {
      campaignCollectionView.topAnchor.constraint(equalTo: self.scrollViewSubView.topAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      campaignCollectionView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      campaignCollectionView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      campaignCollectionView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      campaignCollectionView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      campaignCollectionView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      campaignCollectionView.register(UINib(nibName: "CampaignCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CampaignCollectionViewCell")

      }


      func setPageControl(){
      let pC = UIPageControl()
      pageControl = pC
      pageControl.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(pageControl)

      if #available(iOS 11.0, *) {
      pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
      } else {
      pageControl.topAnchor.constraint(equalTo: campaignCollectionView.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      pageControl.heightAnchor.constraint(equalToConstant: 10).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      pageControl.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      pageControl.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      pageControl.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      pageControl.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      //pageControl.backgroundColor = .black
      //pageControl.tintColor = .white
      pageControl.pageIndicatorTintColor = .lightGray
      pageControl.currentPageIndicatorTintColor = .black

      pageControl.numberOfPages = 3
      pageControl.currentPage = 0
      //pageControl.hidesForSinglePage = true
      }

      func winGiftVoucherCodeFunc(){

      let wGVC : WinGiftVoucherView = UIView.fromNib()
      winGiftVoucherCode = wGVC
      winGiftVoucherCode.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(winGiftVoucherCode)

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
      } else {
      winGiftVoucherCode.topAnchor.constraint(equalTo: pageControl.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      winGiftVoucherCode.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      winGiftVoucherCode.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      winGiftVoucherCode.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      winGiftVoucherCode.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      let rate : CGFloat = 0.85

      let mainRect = winGiftVoucherCode.frame
      let rec = winGiftVoucherCode.progressX.frame
      let aa = ProgressView(frame: CGRect(x: 0, y: 0, width: ( mainRect.size.width - 100 ) * rate , height: rec.size.height) )
      aa.backgroundColor = UIColor.black
      self.winGiftVoucherCode.progressX.addSubview(aa)

      winGiftVoucherCode.voucherRemainderViewLeftConstraint.constant = ( winGiftVoucherCode.frame.width - 100 ) * rate - 20


      }

      func lastCallDiscountFunc(){

      let lCDV : LastCallDiscountView = UIView.fromNib()
      lastCallDiscountView = lCDV
      lastCallDiscountView.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(lastCallDiscountView)

      if #available(iOS 11.0, *) {
      lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
      } else {
      lastCallDiscountView.topAnchor.constraint(equalTo: winGiftVoucherCode.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      }

      func lastCallDiscountFunc2(){

      let lCDV : LastCallDiscountView = UIView.fromNib()
      lastCallDiscountView2 = lCDV
      lastCallDiscountView2.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(lastCallDiscountView2)

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
      } else {
      lastCallDiscountView2.topAnchor.constraint(equalTo: lastCallDiscountView.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView2.heightAnchor.constraint(equalToConstant: 150).isActive = true
      // campaignCollectionView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView2.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView2.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView2.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      }

      func lastCallDiscountFunc3(){

      let lCDV : LastCallDiscountView = UIView.fromNib()
      lastCallDiscountView3 = lCDV
      lastCallDiscountView3.translatesAutoresizingMaskIntoConstraints = false
      self.scrollViewSubView.addSubview(lastCallDiscountView3)

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
      } else {
      lastCallDiscountView3.topAnchor.constraint(equalTo: lastCallDiscountView2.bottomAnchor , constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
      } else {
      lastCallDiscountView3.heightAnchor.constraint(equalToConstant: 150).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.leadingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.leadingAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView3.leadingAnchor.constraint(equalTo: self.scrollViewSubView.leadingAnchor, constant: 10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.trailingAnchor.constraint(equalTo: scrollViewSubView.safeAreaLayoutGuide.trailingAnchor, constant: -10).isActive = true
      } else {
      lastCallDiscountView3.trailingAnchor.constraint(equalTo: self.scrollViewSubView.trailingAnchor, constant: -10).isActive = true
      }

      if #available(iOS 11.0, *) {
      lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.safeAreaLayoutGuide.bottomAnchor, constant: 10).isActive = true
      } else {
      lastCallDiscountView3.bottomAnchor.constraint(greaterThanOrEqualTo: self.scrollViewSubView.bottomAnchor, constant: 10).isActive = true
      }

      }






      ios swift uiscrollview xib nib






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 8:54









      codeByTheycodeByThey

      775526




      775526
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Firstly, I strongly urge you to set up all your UI in XIBs/storyboards. It's so much easier to see what you're doing, rather than adding views programmatically.



          If a scroll view doesn't scroll it probably means that its contentSize is smaller than its frame. It could also mean that you're blocking touches to it, which may be caused by your UICollectionView. Embedding a scrollable view inside a scrollable view will cause you headaches.



          If you want to properly set up scroll views using autolayout I recommend following this article. The core principle is:




          1. Add the scroll view to the VC's view and set its constraints to match the edges (leading/trailing/top/bottom) of the safe area.

          2. Add a content view (UIView) whose constraints match the edges of the scroll view.

          3. Set the content view's width to match the safeArea's width (assuming you want vertical scrolling, otherwise match the heights).

          4. Add all your views to the content view and ensure that their constraints provide it with a fixed height (again, assuming you're going for vertical scrolling). That means having at least 1 view with a constraint to the top of the content view, and at least 1 with a constraint to its bottom, with set heights and vertical spaces in between.


          It takes a while to learn how to do this properly, so be patient.






          share|improve this answer
























          • Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

            – codeByThey
            Nov 16 '18 at 11:49











          • Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

            – Guy Kogus
            Nov 17 '18 at 7:40












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334387%2fuse-xib-with-scroll-view-swift%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Firstly, I strongly urge you to set up all your UI in XIBs/storyboards. It's so much easier to see what you're doing, rather than adding views programmatically.



          If a scroll view doesn't scroll it probably means that its contentSize is smaller than its frame. It could also mean that you're blocking touches to it, which may be caused by your UICollectionView. Embedding a scrollable view inside a scrollable view will cause you headaches.



          If you want to properly set up scroll views using autolayout I recommend following this article. The core principle is:




          1. Add the scroll view to the VC's view and set its constraints to match the edges (leading/trailing/top/bottom) of the safe area.

          2. Add a content view (UIView) whose constraints match the edges of the scroll view.

          3. Set the content view's width to match the safeArea's width (assuming you want vertical scrolling, otherwise match the heights).

          4. Add all your views to the content view and ensure that their constraints provide it with a fixed height (again, assuming you're going for vertical scrolling). That means having at least 1 view with a constraint to the top of the content view, and at least 1 with a constraint to its bottom, with set heights and vertical spaces in between.


          It takes a while to learn how to do this properly, so be patient.






          share|improve this answer
























          • Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

            – codeByThey
            Nov 16 '18 at 11:49











          • Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

            – Guy Kogus
            Nov 17 '18 at 7:40
















          0














          Firstly, I strongly urge you to set up all your UI in XIBs/storyboards. It's so much easier to see what you're doing, rather than adding views programmatically.



          If a scroll view doesn't scroll it probably means that its contentSize is smaller than its frame. It could also mean that you're blocking touches to it, which may be caused by your UICollectionView. Embedding a scrollable view inside a scrollable view will cause you headaches.



          If you want to properly set up scroll views using autolayout I recommend following this article. The core principle is:




          1. Add the scroll view to the VC's view and set its constraints to match the edges (leading/trailing/top/bottom) of the safe area.

          2. Add a content view (UIView) whose constraints match the edges of the scroll view.

          3. Set the content view's width to match the safeArea's width (assuming you want vertical scrolling, otherwise match the heights).

          4. Add all your views to the content view and ensure that their constraints provide it with a fixed height (again, assuming you're going for vertical scrolling). That means having at least 1 view with a constraint to the top of the content view, and at least 1 with a constraint to its bottom, with set heights and vertical spaces in between.


          It takes a while to learn how to do this properly, so be patient.






          share|improve this answer
























          • Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

            – codeByThey
            Nov 16 '18 at 11:49











          • Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

            – Guy Kogus
            Nov 17 '18 at 7:40














          0












          0








          0







          Firstly, I strongly urge you to set up all your UI in XIBs/storyboards. It's so much easier to see what you're doing, rather than adding views programmatically.



          If a scroll view doesn't scroll it probably means that its contentSize is smaller than its frame. It could also mean that you're blocking touches to it, which may be caused by your UICollectionView. Embedding a scrollable view inside a scrollable view will cause you headaches.



          If you want to properly set up scroll views using autolayout I recommend following this article. The core principle is:




          1. Add the scroll view to the VC's view and set its constraints to match the edges (leading/trailing/top/bottom) of the safe area.

          2. Add a content view (UIView) whose constraints match the edges of the scroll view.

          3. Set the content view's width to match the safeArea's width (assuming you want vertical scrolling, otherwise match the heights).

          4. Add all your views to the content view and ensure that their constraints provide it with a fixed height (again, assuming you're going for vertical scrolling). That means having at least 1 view with a constraint to the top of the content view, and at least 1 with a constraint to its bottom, with set heights and vertical spaces in between.


          It takes a while to learn how to do this properly, so be patient.






          share|improve this answer













          Firstly, I strongly urge you to set up all your UI in XIBs/storyboards. It's so much easier to see what you're doing, rather than adding views programmatically.



          If a scroll view doesn't scroll it probably means that its contentSize is smaller than its frame. It could also mean that you're blocking touches to it, which may be caused by your UICollectionView. Embedding a scrollable view inside a scrollable view will cause you headaches.



          If you want to properly set up scroll views using autolayout I recommend following this article. The core principle is:




          1. Add the scroll view to the VC's view and set its constraints to match the edges (leading/trailing/top/bottom) of the safe area.

          2. Add a content view (UIView) whose constraints match the edges of the scroll view.

          3. Set the content view's width to match the safeArea's width (assuming you want vertical scrolling, otherwise match the heights).

          4. Add all your views to the content view and ensure that their constraints provide it with a fixed height (again, assuming you're going for vertical scrolling). That means having at least 1 view with a constraint to the top of the content view, and at least 1 with a constraint to its bottom, with set heights and vertical spaces in between.


          It takes a while to learn how to do this properly, so be patient.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 11:19









          Guy KogusGuy Kogus

          6,39111927




          6,39111927













          • Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

            – codeByThey
            Nov 16 '18 at 11:49











          • Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

            – Guy Kogus
            Nov 17 '18 at 7:40



















          • Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

            – codeByThey
            Nov 16 '18 at 11:49











          • Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

            – Guy Kogus
            Nov 17 '18 at 7:40

















          Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

          – codeByThey
          Nov 16 '18 at 11:49





          Thanks for repyly. I also applied all topics. But I didnt set contentsize because I give the last element constraint "greaterThanOrEqualTo" so I think no need the content size.

          – codeByThey
          Nov 16 '18 at 11:49













          Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

          – Guy Kogus
          Nov 17 '18 at 7:40





          Unfortunately you can't have any >= heights if you want the scroll view to know the content size. If you really must have that then maybe set the content view height to match the safe area height, but then it kinda defeats the purpose of the scroll view...

          – Guy Kogus
          Nov 17 '18 at 7:40




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53334387%2fuse-xib-with-scroll-view-swift%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine