向以编程方式创建的按钮添加约束

add constraint to programmatically created button

您好,我以编程方式在 tableView 页脚中显示了 UIButton。问题是它在 5s 等较小的手机中无法正确显示。按钮标题在较小的屏幕上是正确的。

您可以在图像中看到按钮标题不在中心

这就是我显示按钮的方式

override func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let footerView = UIView(frame: CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height))


        nextButton   = UIButton(type: UIButtonType.System) as UIButton
        nextButton!.frame = CGRectMake(0, 0, 414, 65)

        nextButton!.setTitle("NEXT", forState: UIControlState.Normal)
        nextButton!.setTitleColor(UIColor.whiteColor(), forState:UIControlState.Normal)
       nextButton!.titleLabel?.font = UIFont(name: Variables.MONTESERRAT_REGULAR, size: 20.0)



          nextButton!.backgroundColor = UIColor().blueColor()       //top
          nextButton!.titleEdgeInsets = UIEdgeInsetsMake(0.0,10.0, 10.0, 0.0)

          nextButton!.addTarget(self, action: "nextButtonClicked:", forControlEvents: UIControlEvents.TouchUpInside)
        footerView.addSubview(nextButton!)


        return footerView
    }

    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 50.0
    }

使用自动布局 (VFL) 而不是设置一些随机的静态框架,

let containerView = UIView()
containerView.translatesAutoresizingMaskIntoConstraints = false
containerView.backgroundColor = UIColor.greenColor()


let buttonNext = UIButton(type: UIButtonType.System)
buttonNext.translatesAutoresizingMaskIntoConstraints = false
buttonNext.setTitle("NEXT", forState: UIControlState.Normal)
//buttonNext.backgroundColor = UIColor.blueColor()

containerView.addSubview(buttonNext)

self.view.addSubview(containerView)

var verticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|[containerView]|", options: [], metrics: nil, views: ["containerView": containerView])

var horizontalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[containerView]|", options: [], metrics: nil, views: ["containerView": containerView])

self.view.addConstraints(verticalConstraint)
self.view.addConstraints(horizontalConstraint)


verticalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|[buttonNext]|", options: [], metrics: nil, views: ["buttonNext": buttonNext])

horizontalConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|[buttonNext]|", options: [], metrics: nil, views: ["buttonNext": buttonNext])

self.view.addConstraints(verticalConstraint)
self.view.addConstraints(horizontalConstraint)

如果需要,您可以设置标题对齐方式。

希望对您有所帮助。

Edit/Update:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView?

    let footerView = UIView()
    footerView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 50)
    footerView.backgroundColor = UIColor.redColor()

    let buttonNext = UIButton(type: UIButtonType.System)
    buttonNext.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 50)
    buttonNext.translatesAutoresizingMaskIntoConstraints = false
    buttonNext.setTitle("NEXT", forState: UIControlState.Normal)
    buttonNext.backgroundColor = UIColor.blueColor()

    footerView.addSubview(buttonNext)

    footerView.layoutIfNeeded()
    return footerView

}

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 50
}