UIButton作为子视图以编程方式约束

时间:2017-03-06 04:51:13

标签: ios swift uibutton autolayout constraints

我在故事板中有一个成功约束的UIScrollView(IBOutlet)。然后我以编程方式创建UIButton并将它们作为子视图放到UIScrollView中。我如何以编程方式设置这些UIButton约束,以便它们的高度和大小与它们的超级视图相符?

class ViewController: UIViewController {

@IBOutlet weak var aScrollView: UIScrollView!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    var xCoord: CGFloat = 5
    let yCoord: CGFloat = 5
    let buttonWidth:CGFloat = 100
    let buttonHeight: CGFloat = 100

    let gapBetweenButtons: CGFloat = 10


    var itemCount = 0

    // MARK: - filter buttons
    for i in 0..<6 {

        itemCount = i

        let aButton = UIButton(type: .custom)
        aButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
        aButton.backgroundColor = UIColor.blue
        aButton.layer.cornerRadius = aButton.frame.size.width / 2
        aButton.clipsToBounds = true

        xCoord += buttonWidth + gapBetweenButtons

        aScrollView.addSubview(aButton)   
    }
}

3 个答案:

答案 0 :(得分:0)

试试这个 -

这只是关于如何以编程方式添加约束的想法。

如需更多了解,请访问以下链接 - https://developer.apple.com/reference/appkit/nslayoutanchor

 let myButton = UIButton()
 self.aScrollView.addSubview(myButton)

 self.view.translatesAutoresizingMaskIntoConstraints = false
 let margins = self.view.layoutMarginsGuide
 myButton.leadingAnchor.constraint(equalTo: forView. aScrollView.leadingAnchor, constant: 5).active = true
 myButton.topAnchor.constraint(equalTo: forView. aScrollView.topAnchor, constant: 5).active = true
 myButton.heightAnchor.constraintEqualToConstant(100.0).active = true
 myButton.widthAnchor.constraintEqualToConstant(100.0).active = true

答案 1 :(得分:0)

将您的代码更改为:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var xCoord: CGFloat = 5
        let yCoord: CGFloat = 5

        let gapBetweenButtons: CGFloat = 10


        let buttonCount = 6
        let buttonWidth = (aScrollView.frame.width - CGFloat(buttonCount - 1) * gapBetweenButtons - xCoord - xCoord) / CGFloat(buttonCount) // - (2 * xCoord) = - (margin left + margin right).
        let buttonHeight = buttonWidth

        var itemCount = 0

        // MARK: - filter buttons
        for i in 0..<buttonCount {

            itemCount = i

            let aButton = UIButton(type: .custom)
            aButton.frame = CGRect(x: xCoord, y: yCoord, width: buttonWidth, height: buttonHeight)
            aButton.backgroundColor = UIColor.blue
            aButton.layer.cornerRadius = aButton.frame.size.width / 2
            aButton.clipsToBounds = true

            xCoord += buttonWidth + gapBetweenButtons

            aScrollView.addSubview(aButton)
        }
    }

答案 2 :(得分:0)

这是怎么做的。我为你制作了一个视频教程add unbutton programmatically to scrollview in iOS, swift。请参阅链接。

  1. 添加滚动视图(添加约束 - &gt;顶部,底部,左侧,右侧)。
  2. 然后在滚动视图中添加视图(添加约束 - &gt;顶部,底部,左侧,右侧,宽度,高度以及根据需要设置宽度和高度。在视频中说明)
  3. 将这两个视图添加为子视图。
  4. 然后以编程方式添加按钮及其约束。
  5. 关注视频教程。 add UIButton(outlet) to scrollview programmatically