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

时间:2016-03-16 08:47:19

标签: ios autolayout ios-autolayout

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

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

这就是我显示按钮的方式

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
    }

1 个答案:

答案 0 :(得分: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)

如果您想要任何特定的标题,可以设置标题对齐。

希望这会对你有所帮助。

修改/更新

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
}