【问题标题】:How to add multiple subviews on view without hiding the last added subview?如何在视图上添加多个子视图而不隐藏最后添加的子视图?
【发布时间】:2016-06-10 14:44:16
【问题描述】:

这是我的代码

  var button = [UIButton?](count:3, repeatedValue: UIButton())
  for i in 0...2{

            button[i]!.tag = i
            button[i]!.frame = CGRectMake(CGFloat(i) *(collectionViewLove?.frame.width)!/3,0,(collectionViewLove?.frame.width)!/3, 30)
            button[i]!.setTitle(titleForButtonsOneSelected[i], forState: .Normal)
            button[i]!.titleLabel?.adjustsFontSizeToFitWidth = true
            button[i]!.layer.borderWidth = 1.0
            button[i]!.layer.borderColor = UIColor.blueColor().CGColor
            button[i]!.backgroundColor = UIColor.clearColor()
            button[i]!.setTitleColor(UIColor.blackColor(), forState: .Normal)
            view.addSubview(button[i]!)

        }

问题是仅显示添加到视图的最后一个按钮。如何在视图中显示所有按钮对象?

编辑: 我得到的 UIButton 的帧值

(0.0, 0.0, 106.666666666667, 30.0)

(106.666666666667, 0.0, 106.666666666667, 30.0)

(213.333333333333, 0.0, 106.666666666667, 30.0)

【问题讨论】:

  • 每次更改按钮框架...特别是 x 和 y
  • 我在上面的代码里改了。
  • 您的按钮是如何创建的?
  • 如您所见,EDIT 中的按钮框架不同。
  • @Eendje var button = [UIButton?](count:3, repeatValue: UIButton())

标签: ios swift uiview uibutton subview


【解决方案1】:
let buttons = Array(0...2).map { number -> UIButton in
    let button = UIButton(frame: CGRectMake(CGFloat(number) * collectionViewLove!.frame.width / 3, 0, collectionViewLove!.frame.width / 3, 30))

    button.tag = number
    button.setTitle(titleForButtonsOneSelected[number], forState: .Normal)
    buttontitleLabel?.adjustsFontSizeToFitWidth = true
    button.layer.borderWidth = 1.0
    button.layer.borderColor = UIColor.blueColor().CGColor
    button.backgroundColor = UIColor.clearColor()
    button.setTitleColor(UIColor.blackColor(), forState: .Normal)
    view.addSubview(button)

    return button
}

这样,buttons 中就有 3 个不同的按钮。您也可以在此处自定义按钮。

编辑:

let buttons = Array(0...2).forEach {
    let button = UIButton(frame: CGRectMake(CGFloat($0) * collectionViewLove!.frame.width / 3, 0, collectionViewLove!.frame.width / 3, 30))

    button.tag = $0
    button.setTitle(titleForButtonsOneSelected[$0], forState: .Normal)
    buttontitleLabel?.adjustsFontSizeToFitWidth = true
    button.layer.borderWidth = 1.0
    button.layer.borderColor = UIColor.blueColor().CGColor
    button.backgroundColor = UIColor.clearColor()
    button.setTitleColor(UIColor.blackColor(), forState: .Normal)
    view.addSubview(button)
}

【讨论】:

  • 按钮数组基本上只有一个按钮对象。我明白了。
  • 对。如果你愿意,我可以用你如何自定义里面的按钮来更新答案。
  • 如果以后不需要访问按钮,也可以使用for-inforEach
【解决方案2】:

看起来button[i]!.frame = CGRectMake(CGFloat(i) *(collectionViewLove?.frame.width)!/3,0,(collectionViewLove?.frame.width)!/3, 30) 每次都生成相同的帧,因此您的按钮将相互叠加。你需要在你的通话中加入一些动态的东西,让它们出现在不同的位置并且可见。

【讨论】:

  • 这不是问题。我打印了 x 和 y,它们正在动态变化。
  • 好的,然后确保就像@Eendje 所说的那样,您实际上是在创建单独的按钮。一个问题可能是您在数组中放置了 3 次相同的按钮,所以它只是被移动到最后一个位置。
猜你喜欢
  • 2015-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多