[英]add constraint to programmatically created button
您好,我以编程UIButton
在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)
您可以根据需要设置标题对齐方式。
希望这对您有所帮助。
编辑/更新:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.