草庐IT

ios - 如何以编程方式在 ScrollView 中添加多个按钮

coder 2023-09-14 原文

我试图在 scrollView 中放置多个按钮,但它只是滚动背景 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()
    }

我为按钮框架写了这段代码 函数 eatDrinkBut​​tonSetup(){

        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!!!

关于ios - 如何以编程方式在 ScrollView 中添加多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262989/

有关ios - 如何以编程方式在 ScrollView 中添加多个按钮的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby - 如何以所有可能的方式将字符串拆分为长度最多为 3 的连续子字符串? - 2

    我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  7. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  8. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  9. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  10. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

随机推荐