UIScrollView for ViewController horizontally and vertically - ios

My code tutorial works for scrolling horizontally with ScrollView.
I would like to make it with ViewControllers (instead of views) from my Storyboard.
And I would like to add a vertical ScrollView (To move from center to right or left = ScrollView horizontal, and to move from center to top or bottom (vertically).
Is that possible? Could someone give me a hint ?
Thanks
For information, this is my code :
import UIKit
class ViewController: UIViewController {
lazy var view0: UIView = {
let view = UIView()
view.backgroundColor = .systemTeal
let label = UILabel()
label.text = "Page 0"
label.textAlignment = .center
view.addSubview(label)
label.edgeTo(view: view)
return view
}()
lazy var view1: UIView = {
let view = UIView()
view.backgroundColor = .systemPink
let label = UILabel()
label.text = "Page 1"
label.textAlignment = .center
view.addSubview(label)
label.edgeTo(view: view)
return view
}()
lazy var view2: UIView = {
let view = UIView()
view.backgroundColor = .systemYellow
let label = UILabel()
label.text = "Page 2"
label.textAlignment = .center
view.addSubview(label)
label.edgeTo(view: view)
return view
}()
lazy var views = [view0, view1, view2]
lazy var scrollView: UIScrollView = {
let scrollView = UIScrollView()
scrollView.showsHorizontalScrollIndicator = false
scrollView.isPagingEnabled = true
scrollView.contentSize = CGSize(width: view.frame.width * CGFloat(views.count), height: view.frame.height)
for i in 0..<views.count {
scrollView.addSubview(views[i])
views[i].frame = CGRect(x: view.frame.width * CGFloat(i), y: 0, width: view.frame.width, height: view.frame.height)
}
scrollView.delegate = self
return scrollView
}()
lazy var pageControl: UIPageControl = {
let pageControl = UIPageControl()
pageControl.numberOfPages = views.count
pageControl.currentPage = 0
pageControl.addTarget(self, action: #selector(pageControlTapHandler(sender:)), for: .touchUpInside)
return pageControl
}()
#objc
func pageControlTapHandler(sender: UIPageControl) {
scrollView.scrollTo(horizontalPage: sender.currentPage, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(scrollView)
scrollView.edgeTo(view: view)
view.addSubview(pageControl)
pageControl.pinTo(view)
}
}
extension ViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageIndex = round(scrollView.contentOffset.x / view.frame.width)
pageControl.currentPage = Int(pageIndex)
}

Related

How should I set constraints to the subviews of my tableHeaderView?

I have this viewController:
class CreateSkillGroupViewController: UIViewController {
lazy var headerStack: UIStackView = {
let stack = UIStackView(frame: CGRect(x: 0, y: 0, width: 20, height: 400))
stack.axis = .vertical
let titleField = UITextView(frame: CGRect(x: 0, y: 0, width: 300, height: 88))
titleField.backgroundColor = .green
titleField.snp.makeConstraints{ (make) in
make.height.equalTo(50)
}
let descriptionField = UITextView(frame: CGRect(x: 0, y: 0, width: 300, height: 120))
descriptionField.snp.makeConstraints{ (make) in
make.height.equalTo(100)
}
let headerImage = UIImageView(image: UIImage(named: "AppIcon-bw"))
headerImage.snp.makeConstraints{ (make) in
make.height.equalTo(300)
make.width.equalTo(200)
}
stack.addArrangedSubview(headerImage)
stack.addArrangedSubview(titleField)
stack.addArrangedSubview(descriptionField)
stack.backgroundColor = .blue
return stack
}()
override func viewDidLoad() {
super.viewDidLoad()
configureNavigationItem()
skillsTableView = UITableView(frame: .zero, style: .insetGrouped)
skillsTableView.register(SkillSummaryCell.self)
skillsTableView.tableHeaderView = headerStack
view.addSubview(skillsTableView)
skillsTableView.tableHeaderView?.snp.makeConstraints{ (make) in
make.top.equalToSuperview()
make.left.equalToSuperview()
make.right.equalToSuperview()
make.width.equalToSuperview()
make.height.equalTo(400)
}
skillsTableView.snp.makeConstraints{ (make) in
make.edges.equalToSuperview()
}
...
This is what it creates...
As you can see I use the lazy var headerStack to setup the tableHeaderView which is a stackView. As you can see all of the constraints in that stack view are explicit number sizes. Then in the viewDidLoad, I add the constraints for the tableView itself.
I want to know how I would for instance, center the headerImage in the viewController, or in the tableView for that matter or make its width half of the tableView's width. I cannot set equalToSuperView because the view hasn't been laid out yet. And once its laid out, I cannot access the stack view subviews to retroactively add constraints to them.
First of all, I wouldn't use a stackView as a tableHeaderView because you need your tableHeaderView to be the same width as the tableView. Embed your stackView in a view and use that view as the header. Ensure that header remains the width of the tableView regardless of the stackView content.
Also, it looks like you are trying to mix autolayout with frame-based layout and that's gonna get you into trouble. I'm not sure why you were setting frames on some of your subviews.
Pay attention to how you define stackView.alignment and stackView.distribution. I'm not sure what your goal is so it's hard to give you much advice there. Bit I assume you want your subviews centered and to have their own unique width.
You defined a lot of your subviews in your stackView builder and that got you into trouble. Ensure that you have one builder for each subview. It helps keep your code clean.
Lastly, you can use autolayout to define the width equal to the width of the tableView. There are a lot of solutions on the web that make you compute the frames for your header manually and that's just a pain.
I changed some names around added some colors but I think this will help you:
extension UIColor {
static let headerImage = UIColor.systemPurple
static let header = UIColor.systemPink
static let titleField = UIColor.white
static let descriptionField = UIColor.systemYellow
static let headerStack = UIColor.systemOrange
static let tableView = UIColor.systemMint
}
class ViewController: UIViewController {
lazy var headerImage: UIImageView = {
let headerImage = UIImageView(image: UIImage(systemName: "checkmark"))
headerImage.translatesAutoresizingMaskIntoConstraints = false
headerImage.backgroundColor = .headerImage
return headerImage
}()
lazy var headerView: UIView = {
let header = UIView()
header.backgroundColor = .header
header.translatesAutoresizingMaskIntoConstraints = false
return header
}()
lazy var titleField: UITextView = {
let titleField = UITextView(frame: .zero)
titleField.translatesAutoresizingMaskIntoConstraints = false
titleField.backgroundColor = .titleField
return titleField
}()
lazy var descriptionField: UITextView = {
let descriptionField = UITextView(frame: .zero)
descriptionField.translatesAutoresizingMaskIntoConstraints = false
descriptionField.backgroundColor = .descriptionField
return descriptionField
}()
lazy var headerStack: UIStackView = {
let stack = UIStackView(frame: .zero)
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
stack.distribution = .fillProportionally
stack.alignment = .center
stack.spacing = 10
stack.backgroundColor = .headerStack
return stack
}()
lazy var tableView: UITableView = {
let tableView = UITableView(frame: .zero, style: .insetGrouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(SkillSummaryCell.self, forCellReuseIdentifier: "SkillSummaryCell")
tableView.backgroundColor = .tableView
tableView.delegate = self
tableView.dataSource = self
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
addViews()
arrangeViews()
tableView.layoutIfNeeded()
}
func addViews() {
view.addSubview(tableView)
headerStack.addArrangedSubview(headerImage)
headerStack.addArrangedSubview(titleField)
headerStack.addArrangedSubview(descriptionField)
headerView.addSubview(headerStack)
tableView.tableHeaderView = headerView
}
func arrangeViews() {
tableView.snp.makeConstraints{ (make) in
make.edges.equalTo(view.safeAreaLayoutGuide)
}
descriptionField.snp.makeConstraints{ (make) in
make.height.equalTo(100)
make.width.equalTo(300)
}
titleField.snp.makeConstraints{ (make) in
make.height.equalTo(100)
make.width.equalTo(300)
}
headerStack.snp.makeConstraints { make in
make.top.equalToSuperview()
make.bottom.equalToSuperview()
make.centerX.equalToSuperview()
}
headerView.snp.makeConstraints { make in
make.width.equalTo(tableView)
}
headerImage.snp.makeConstraints{ (make) in
make.width.equalTo(tableView).dividedBy(2)
make.height.equalTo(headerImage.snp.width)
}
}
}
use it:
viewForHeaderInSection
as
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let tableBounds = tableView.bounds // <- table size
let sectionIndex = section // <- Section index
}
In this method, you can customize the header for a specific section, and take into account the size of your table
ALSO:
You can use UIScreen.main.bounds - get the screen size of your phone at any time, this can be very useful, especially considering that tables are often equal in width to the width of the screen

I cannot stretch the height of the titleView in the UINavigationBar

This is my code:
class UINavigationControllerCustom : UINavigationController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews();
navigationBar.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: 120);
}
}
class PreferenceInput: UIViewController {
override func viewDidLoad() {
let l1 = UILabel()
l1.backgroundColor = .clear
l1.numberOfLines = 1
l1.font = UIFont.boldSystemFont(ofSize: 30.0)
l1.textAlignment = .left
l1.textColor = .black
l1.text = "Bold title"
l1.sizeToFit();
let l2 = UILabel()
l2.backgroundColor = .clear
l2.numberOfLines = 2
l2.font = UIFont.boldSystemFont(ofSize: 16.0)
l2.textAlignment = .left
l2.textColor = .darkGray;
l2.text = "This is a\nmultiline string for the navBar"
l2.sizeToFit();
let tView = UIStackView(arrangedSubviews: [l1,l2]);
tView.axis = .vertical;
let spacer = UIView()
let constraint = spacer.widthAnchor.constraint(greaterThanOrEqualToConstant: CGFloat.greatestFiniteMagnitude)
constraint.isActive = true
constraint.priority = .defaultLow
let stack = UIStackView(arrangedSubviews: [tView, spacer])
stack.axis = .horizontal
navigationItem.titleView = stack
navigationItem.titleView.sizeToFit();
}
}
And this is what I get:
I can set the height of the navigation bar but how and where should I set the constraint to stretch the height in respect of the given 120px of the custom navigation controller?
I want to provide an individual height of the navigation bar.

How do implement UIScrollView and UIPageControl Programmatically in Swift4?

Recently I want to implement the UI of Image and text slide show, because of my personal reasons: App pages are implemented by program code. I spent some time researching the usage of UIScrollView and UIPageControl, and found this link(How to create a Scroll View with a page control using swift?) is very informative, but the code is in My environment is not working, the SubView of UIScrollView has not been displayed correctly, and there is a problem with sliding to switch pages. I spent a lot of time searching for the answers to the questions, and I didn't find any valid information.
update my solution:
After some practice, the problem is solved, I will post the notes and solutions.
Just remember:
1、Your UIScrollView must have left (or leading) / top / width / height values.
2、The content elements of your UIScrollView need define the bounds of the contentSize-but they do so with the bottom & right (trailing) constraints.
3、The middle SubView of each UIScrollView requires some left (or leading) and right (trailing) constraints. See the code example for details.
class NTDummyVC: UIViewController, UIScrollViewDelegate {
let SCROLLVIEW_HIGHT:CGFloat = 500
let PAGECONTROL_WIDTH:CGFloat = 200
let PAGECONTROL_HIGHT:CGFloat = 50
var scrollView = UIScrollView()
var pageControl = UIPageControl()
var contentViewArray: NSMutableArray = NSMutableArray()
var imageArray: NSArray = ["ImageName one",
"ImageName two",
"ImageName three"]
var titleArray: NSArray = [NSLocalizedString("Title one", comment: ""),
NSLocalizedString("Title two", comment: ""),
NSLocalizedString("Title three", comment: "")]
var subTitleArray: NSArray = [NSLocalizedString("SubTitle one", comment: ""),
NSLocalizedString("SubTitle two", comment: ""),
NSLocalizedString("SubTitle three", comment: "")]
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
initScrollView()
initPageControl()
loadScrollView()
//After loading all SubViews, update the contentSize of UIScrollView according to the number of Subviews.
let scrollViewWidth = scrollView.frame.size.width * CGFloat(getPageCount())
self.scrollView.contentSize = CGSize(width: scrollViewWidth, height: scrollView.frame.size.height)
}
func getTopbarHeight() -> CGFloat {
let statusBarHeight = UIApplication.shared.statusBarFrame.height
let navigationBarHeight = navigationController?.navigationBar.frame.height ?? 0.0
return statusBarHeight + navigationBarHeight
}
func getPageCount() -> Int {
var pageCount = 0
pageCount = imageArray.count
if titleArray.count < pageCount {
pageCount = titleArray.count
}
if subTitleArray.count < pageCount {
pageCount = subTitleArray.count
}
return pageCount
}
func initScrollView() {
self.scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: SCROLLVIEW_HIGHT))
self.scrollView.delegate = self
self.scrollView.bounces = false
self.scrollView.isPagingEnabled = true
self.scrollView.showsVerticalScrollIndicator = false
self.scrollView.showsHorizontalScrollIndicator = false
self.view.addSubview(self.scrollView)
//Constraints for ScrollView, by snapkit tool.
self.scrollView.snp.makeConstraints { (make) in
make.width.equalToSuperview()
make.height.equalTo(SCROLLVIEW_HIGHT)
make.top.equalToSuperview().offset(getTopbarHeight())
make.leading.equalToSuperview()
}
}
func initPageControl() {
self.pageControl.numberOfPages = getPageCount()
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.gray
self.pageControl.pageIndicatorTintColor = UIColor.gray
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.view.addSubview(pageControl)
//Constraint displayed in the center, by snapkit tool.
self.pageControl.snp.makeConstraints { (make) in
make.width.equalTo(PAGECONTROL_WIDTH)
make.height.equalTo(PAGECONTROL_HIGHT)
make.top.equalTo(self.scrollView.snp.bottom).offset(10)
make.leading.equalToSuperview()
make.trailing.equalToSuperview()
make.centerX.equalToSuperview()
}
pageControl.addTarget(self, action: #selector(self.changePage(sender:)), for: UIControlEvents.valueChanged)
}
func loadScrollView() {
let pageCount = getPageCount()
for index in 0...(pageCount - 1) {
var frame = CGRect.zero
frame.origin.x = self.scrollView.frame.size.width * CGFloat(index)
frame.origin.y = 0
frame.size = self.scrollView.frame.size
let contentView = UIView(frame: frame)
contentView.backgroundColor = .white
self.scrollView.addSubview(contentView)
//Constraints for SubView, by snapkit tool.
//Do’t add extra constraints such as top, bottom, width, height, etc., which will cause abnormal display.
contentView.snp.makeConstraints { (make) in
make.width.equalTo(UIUtils.screenWidth())
make.height.equalTo(self.scrollView.snp.height)
if index >= 1 {
let view:UIView = contentViewArray.object(at: index - 1) as! UIView
make.leading.equalTo(view.snp.trailing)
} else {
make.leading.equalTo(0)
}
if index == (pageCount - 1) {
make.trailing.equalTo(0)
}
}
let imageView = UIImageView(image: UIImage(named: imageArray[index] as! String))
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = UIViewContentMode.scaleAspectFit
contentView.addSubview(imageView)
imageView.snp.makeConstraints { (make) in
make.centerX.equalToSuperview()
}
let titleLabel = UILabel()
titleLabel.font = UIFont.boldSystemFont(ofSize: 18)
titleLabel.text = titleArray[index] as? String
titleLabel.textColor = .black
titleLabel.numberOfLines = 0
titleLabel.lineBreakMode = .byWordWrapping
titleLabel.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(titleLabel)
titleLabel.snp.makeConstraints { (make) in
make.top.equalTo(imageView.snp.bottom).offset(30)
make.leading.equalToSuperview().offset(30)
make.trailing.equalToSuperview().offset(-30)
}
let subTitleLabel = UILabel()
subTitleLabel.font = UIFont.boldSystemFont(ofSize: 12)
subTitleLabel.text = subTitleArray[index] as? String
subTitleLabel.textColor = UIUtils.color(2)
subTitleLabel.numberOfLines = 0
subTitleLabel.lineBreakMode = .byWordWrapping
subTitleLabel.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(subTitleLabel)
subTitleLabel.snp.makeConstraints { (make) in
make.top.equalTo(titleLabel.snp.bottom).offset(5)
make.leading.equalToSuperview().offset(30)
make.trailing.equalToSuperview().offset(-30)
}
if (contentViewArray.contains(contentView)) {
contentViewArray.replaceObject(at: contentViewArray.index(of: contentView), with: contentView)
} else {
contentViewArray.insert(contentView, at: index)
}
}
}
// MARK : To Change while clicking on page control
#objc func changePage(sender: AnyObject) -> () {
let x = CGFloat(pageControl.currentPage) * scrollView.frame.size.width
scrollView.setContentOffset(CGPoint(x:x, y:0), animated: true)
}
// MARK : When UIScrollView slides over, update PageControl
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(scrollView.contentOffset.x / scrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
}
Hope to help people who encounter the same problem.

Trying to programmatically add UIScrollView to my App and I keep getting a blank View

Trying to create a UIScrollView and I cant seem to get my labels to appear as I would like them dead center.
lazy var label : UILabel = {
let label = UILabel()
label.text = "center of container view.center of container view"
label.font = UIFont(name: "Bebas Neue", size: 23)!
label.textColor = .black
return label
}()
// Mark: Properties
lazy var contentViewSize = CGSize(width: self.view.frame.width + 1200, height: self.view.frame.height)
// Mark: Views
fileprivate lazy var parentScrollView : UIView = {
var newView = UIView()
newView.translatesAutoresizingMaskIntoConstraints = false
newView.backgroundColor = .black
newView.frame = self.scrollView.bounds
newView.frame.size = contentViewSize
return newView
}()
fileprivate lazy var scrollView : UIScrollView = {
var uiScrollView = UIScrollView(frame: .zero)
uiScrollView.translatesAutoresizingMaskIntoConstraints = false
uiScrollView.frame = view.bounds
uiScrollView.backgroundColor = .gray
uiScrollView.addViewBorder(borderColor: UIColor.gray.cgColor, borderWith: 10, borderCornerRadius: 0)
// uiScrollView.alpha =
uiScrollView.contentSize = contentViewSize
// uiScrollView.autoresizingMask = .flexibleHeight
uiScrollView.showsVerticalScrollIndicator = true
uiScrollView.bounces = true
return uiScrollView
}()
fileprivate lazy var tutorialView : UIStackView = {
var tutView = UIStackView(arrangedSubviews: [label,label,label])
tutView.axis = .horizontal
tutView.backgroundColor = .white
tutView.distribution = .fillEqually
tutView.spacing = 0
return tutView
}()
override func viewDidLoad() {
view.backgroundColor = .white
view.addSubview(scrollView)
scrollView.addSubview(parentScrollView)
parentScrollView.addSubview(tutorialView)
scrollView.widthAnchor.constraint(equalTo:self.view.widthAnchor,multiplier: 0.9).isActive = true
scrollView.heightAnchor.constraint(equalTo:self.view.heightAnchor,multiplier: 0.7).isActive = true
scrollView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 75).isActive = true
tutorialView.topAnchor.constraint(equalTo: parentScrollView.topAnchor,constant: +10).isActive = true}
It seems that when I add that last constraight with the topAnchor I get a blank screen. When I take that off I actually get the borders between the two views and I am able to do some scrolling.
Any help would be appreciated. Thank you
Did you give a value to the scrollView?
uiScrollView.contentSize.width = 1200
Then give the tutorialView the same width constraint with the other constraints.
NSLayoutConstraint.activate([
tutorialView.widthAnchor.constraint(equalToConstant: 1200),
tutorialView.heightAnchor.constraint(equalTo: uiScrollView.heightAnchor)
])
You have to add tutView.translatesAutoresizingMaskIntoConstraints = false when giving layout constraints in closure of tutorialView and also there are some mistakes in your code. So included some changes in your code and check updated code:
lazy var label : UILabel = {
let label = UILabel()
label.text = "center of container view.center of container view"
label.font = UIFont(name: "Bebas Neue", size: 23)!
label.textColor = .green
return label
}()
// Mark: Properties
lazy var contentViewSize = CGSize(width: self.view.frame.width + 1200, height: self.view.frame.height)
// Mark: Views
fileprivate lazy var parentScrollView : UIView = {
var newView = UIView()
newView.translatesAutoresizingMaskIntoConstraints = false
newView.backgroundColor = UIColor.clear
// newView.layer.borderWidth = 2
// newView.layer.borderColor = UIColor.yellow.cgColor
return newView
}()
fileprivate lazy var scrollView : UIScrollView = {
var uiScrollView = UIScrollView()
uiScrollView.translatesAutoresizingMaskIntoConstraints = false
// uiScrollView.frame = view.bounds
uiScrollView.backgroundColor = .orange
uiScrollView.layer.borderColor = UIColor.blue.cgColor
uiScrollView.layer.borderWidth = 2
uiScrollView.layer.cornerRadius = 0
// uiScrollView.alpha =
uiScrollView.contentSize = contentViewSize
// uiScrollView.autoresizingMask = .flexibleHeight
uiScrollView.showsVerticalScrollIndicator = true
uiScrollView.bounces = true
return uiScrollView
}()
fileprivate lazy var tutorialView : UIStackView = {
var tutView = UIStackView(arrangedSubviews: [label,label,label])
tutView.axis = .horizontal
tutView.backgroundColor = .red
tutView.distribution = .fill
tutView.translatesAutoresizingMaskIntoConstraints = false
return tutView
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(scrollView)
scrollView.addSubview(parentScrollView)
parentScrollView.addSubview(tutorialView)
scrollView.widthAnchor.constraint(equalTo:self.view.widthAnchor,multiplier: 0.9).isActive = true
scrollView.heightAnchor.constraint(equalTo:self.view.heightAnchor,multiplier: 0.7).isActive = true
scrollView.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
scrollView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 25).isActive = true
parentScrollView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor).isActive = true
parentScrollView.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 10).isActive = true
parentScrollView.heightAnchor.constraint(equalToConstant: 200).isActive = true
parentScrollView.widthAnchor.constraint(equalToConstant: 200).isActive = true
// scrollView.layer.borderWidth = 2
// scrollView.layer.borderColor = UIColor.blue.cgColor
tutorialView.heightAnchor.constraint(equalTo: parentScrollView.heightAnchor).isActive = true
tutorialView.widthAnchor.constraint(equalToConstant: 1200).isActive = true
}

UIScrollView didn't scrolling?

I'm creating a UIScrollView from code.The only problem is the scrolling is not working go through lot's of the post but didn't find anything.
Here is my code Any help greatly Appreciated.
class ShopDetailviewController : UIViewController {
var MainScrollView : UIScrollView?
let viewForScrollview : UIView = {
let view = UIView()
view.backgroundColor = UIColor.darkGray
return view
}()
let shopNameandAddresView : UIView = {
let view = UIView()
view.backgroundColor = UIColor.black
return view
}()
let timePaymentDetail : UIView = {
let view = UIView()
view.backgroundColor = UIColor.blue
return view
}()
let shopImage : UIImageView = {
let shopeImage = UIImageView()
shopeImage.backgroundColor = UIColor.green
return shopeImage
}()
let ratingStarLable : UILabel = {
let ratingStratLable = UILabel()
ratingStratLable.backgroundColor = UIColor.red
ratingStratLable.text = "3.5"
return ratingStratLable
}()
let shopName : UILabel = {
let shopName = UILabel()
shopName.backgroundColor = UIColor.gray
shopName.text = "Kimling"
return shopName
}()
let shopLocation : UILabel = {
let shopLocation = UILabel()
shopLocation.backgroundColor = UIColor.white
shopLocation.text = "Kondhwa"
return shopLocation
}()
let seprater : UIView = {
let seprater = UIView()
seprater.backgroundColor = UIColor.white
return seprater
}()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
navigationController?.navigationBar.tintColor = .white
view.backgroundColor = UIColor.white
}
override func viewDidLoad() {
super.viewDidLoad()
MainScrollView = UIScrollView()
MainScrollView?.isScrollEnabled = true
MainScrollView?.indicatorStyle = .default
MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)
MainScrollView?.backgroundColor = UIColor.purple
setupViews()
}
func setupViews(){
view.addSubview(MainScrollView!)
MainScrollView?.addSubview(viewForScrollview)
viewForScrollview.addSubview(shopImage)
viewForScrollview.addSubview(shopNameandAddresView)
viewForScrollview.addSubview(timePaymentDetail)
addConstraintsWithFormat("H:|-0-[v0(414)]-0-|", views: MainScrollView!)
addConstraintsWithFormat("V:|-0-[v0(8000)]", views: MainScrollView!)
addConstraintsWithFormat("H:|-0-[v0(414)]-0-|", views: viewForScrollview)
addConstraintsWithFormat("V:|-0-[v0(8000)]", views: viewForScrollview)
addConstraintsWithFormat("V:|-0-[v0(1000)]-0-[v1(1000)]", views: shopImage,shopNameandAddresView)
addConstraintsWithFormat("H:|-0-[v0]-0-|", views: shopImage)
addConstraintsWithFormat("H:|-0-[v0]-0-|", views: shopNameandAddresView)
}
}
Use as this
override func viewDidLayoutSubviews()
{
MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)
}
It seems like the in viewDidLoad layout is not finished so the scrollView wont scroll because it has no proper contentSize try setting the contentSize in ViewWillAppear as
MainScrollView?.contentSize = CGSize(width:self.view.bounds.width, height: 8000)

Resources