如何以编程方式在滚动视图中添加多个按钮



我正在尝试在滚动视图中放置多个按钮,但它只是滚动背景视图。

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()
    }

我为按钮框架编写了这段代码 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)
    }
}

我也尝试这样,但它只是滚动一个按钮。

scrollView.addSubview(attractivePlaceButtonSetup)
self.view.addSubview(scrollView)
    //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!!!

最新更新