2

I am trying to put multiple buttons in a scrollView but it just scrolls background view.

class HomeVC: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Home"

        let scrollView = UIScrollView()

        let view = UIView()
        scrollView.frame =  self.view.bounds
        self.view.backgroundColor = .green
        scrollView.backgroundColor = .blue
        scrollView.addSubview(view)
        self.view.addSubview(scrollView)
        scrollView.isPagingEnabled = true
        scrollView.contentSize = CGSize(width: self.view.frame.size.width, height: self.view.frame.size.height * 3)
        view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height)
        view.backgroundColor = .yellow

        attractivePlaceButtonSetup()
        eatDrinkButtonSetup()
        ShoppingButtonSetup()
        festivalEventButtonSetup()
        hotelGuestHouseButtonSetup()
        travellerEssentialButtonSetup()
        dealButtonSetup()
        seeDoButtonSetup()
    }

I wrote this code for button frame func eatDrinkButtonSetup(){

        let button = UIButton()
        button.frame = CGRect(x: 5, y: 225, width: self.view.frame.size.width - 10, height: 150)
        button.setTitle("Eat & Drink", for: .normal)
        button.setBackgroundImage(#imageLiteral(resourceName: "imageName"), for: .normal)
        button.titleEdgeInsets = UIEdgeInsets(top: -120, left: -200, bottom: 0, right: 0)
          button.addTarget(self, action: #selector(targetEatDrink), for: .touchUpInside)

        view.addSubview(button)

    }

}

I also try to such way but it just scroll a button.

scrollView.addSubview(attractivePlaceButtonSetup)
self.view.addSubview(scrollView)
16
  • Why are you setting view frame y to self.view.frame.size.height ? in line view.frame = CGRect(x: 0, y: self.view.frame.size.height, width: self.view.frame.size.width, height: self.view.frame.size.height) Commented Apr 6, 2017 at 18:26
  • @JasmeetKaur Kaur sorry I have no idea about it Commented Apr 6, 2017 at 18:33
  • What are your ButtonSetup() functions doing? Can you also show the code at least one of them? Commented Apr 6, 2017 at 18:34
  • Is there any specific reason you are using scrollview for add multiple buttons ?This can achieved easily using UICollectionView with Collection cell. Commented Apr 6, 2017 at 18:34
  • @iOSFreak I know that, this way I felt a little comfortable Commented Apr 6, 2017 at 18:36

1 Answer 1

3
    //Try this...
    //Background Scroll Creation

            var stickersScrollViewCount = 0
            func stickersScrollContents() {

                var xCoord: CGFloat = 5
                let yCoord: CGFloat = 5
                let buttonWidth:CGFloat = 45.0
                let buttonHeight: CGFloat = 45.0
                let gapBetweenButtons: CGFloat = 5

                for i in 0..<stickersImageArray.count{
                    stickersScrollViewCount = i
                    // Button properties
                    let filterButton = UIButton(type: .custom)
                    filterButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
                    filterButton.tag = stickersScrollViewCount
                    filterButton.backgroundColor = UIColor.clear
                    filterButton.setTitleColor(UIColor.white, for: .normal)
                    filterButton.titleLabel?.adjustsFontSizeToFitWidth = true
                    filterButton.showsTouchWhenHighlighted = true
                    let myimage = UIImage(named: stickersImageArray[stickersScrollViewCount])
                    filterButton.setImage(myimage, for: .normal)
                    filterButton.addTarget(self, action:#selector(StickersActionTapped), for: .touchUpInside)
                    filterButton.layer.cornerRadius = 5
                    filterButton.clipsToBounds = true
                    xCoord +=  buttonWidth + gapBetweenButtons
                    bgScrollView.addSubview(filterButton)

                }
                bgScrollView.contentSize = CGSize(width: buttonWidth * CGFloat(stickersScrollViewCount+2), height: yCoord)

            }

//Call the function where ever you want viewDidLoad() or on Button Click!!

//Hope this helps!!!
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.