Seb*_*Seb 5 objective-c uitableview ios swift
我试图将一个UIButton浮动到一个普遍的TableView的节头中的右边.到目前为止,我只设法为一个屏幕尺寸添加约束...
到目前为止,这是我的代码:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerFrame:CGRect = tableView.frame
let titelArr: NSArray = ["1", "2", "3", "4", "5", "6"]
var title = UILabel(frame: CGRectMake(10, 10, 100, 30))
title.font = UIFont.boldSystemFontOfSize(20.0)
title.text = titelArr.objectAtIndex(section) as? String
title.textColor = UIColor.whiteColor()
var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton
headBttn.frame = CGRectMake(320, 10, 30, 30)
headBttn.enabled = true
headBttn.titleLabel?.text = title.text
headBttn.tag = titelArr.indexOfObject(titelArr.objectAtIndex(section))
headBttn.addTarget(self, action: "addItem:", forControlEvents: UIControlEvents.TouchUpInside)
var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height))
headerView.backgroundColor = UIColor(red: 108/255, green: 185/255, blue: 0/255, alpha: 0.9)
headerView.addSubview(title)
headerView.addSubview(headBttn)
return headerView
}
Run Code Online (Sandbox Code Playgroud)
如何使按钮浮动正确?其余的约束可以保持原样......
THX为您提供帮助!
//勒布
感谢@rdelmar和一些研究,如果任何人应该被强迫的话,这是答案;-)
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var headerFrame = tableView.frame
var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height))
headerView.backgroundColor = UIColor(red: 108/255, green: 185/255, blue: 0/255, alpha: 0.9)
var title = UILabel()
title.setTranslatesAutoresizingMaskIntoConstraints(false)
title.font = UIFont.boldSystemFontOfSize(20.0)
title.text = titelArr.objectAtIndex(section) as? String
title.textColor = UIColor.whiteColor()
headerView.addSubview(title)
var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton
headBttn.setTranslatesAutoresizingMaskIntoConstraints(false)
headBttn.enabled = true
headBttn.titleLabel?.text = title.text
headBttn.tag = titelArr.indexOfObject(titelArr.objectAtIndex(section))
headBttn.addTarget(self, action: "addItem:", forControlEvents: UIControlEvents.TouchUpInside)
headerView.addSubview(headBttn)
var viewsDict = Dictionary <String, UIView>()
viewsDict["title"] = title
viewsDict["headBttn"] = headBttn
headerView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
"H:|-10-[title]-[headBttn]-15-|", options: nil, metrics: nil, views: viewsDict))
headerView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-[title]-|", options: nil, metrics: nil, views: viewsDict))
headerView.addConstraints(
NSLayoutConstraint.constraintsWithVisualFormat(
"V:|-[headBttn]-|", options: nil, metrics: nil, views: viewsDict))
return headerView
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6312 次 |
| 最近记录: |