您好我已在编程方式UIButton
页脚中显示tableView
。问题是它在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
}
答案 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
}