2015-01-10 76 views
5

我试图将一个UIButton对齐到一个TableView部分头部的右边。 到目前为止,我只设法增加一个屏幕大小的限制......make tableview section section header float/align right to programmatically // Swift

这里是我到目前为止的代码:

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 

} 

我怎样才能使按钮浮动权?其余的约束可以保持原样...

THX为您提供帮助!

//勒布

+0

“float right”是什么意思?你说你已经“为一个屏幕大小增加了限制”,但是你根本没有添加任何限制。这里没有任何代码增加了任何约束(如果通过约束,你的意思是NSLayoutConstraints)。 – rdelmar

+0

对不起,我是新来的快!所以实际上我不知道如何以编程方式设置约束。我希望按钮位于每台设备的部分标题的右侧。使用上面的代码'CGRectMake(320,10,30,30)'该按钮有一个固定的位置,恰好在iPhone 6肖像的右侧。 ;-) – Seb

+0

如果你不知道该怎么做,那么你应该阅读NSLayoutConstraint类的引用,它将包含你需要通过编程方式添加约束的swift和objective-C方法。您应该尝试编写代码,如果遇到困难,请回复一个更具体的问题。 – rdelmar

回答

8

THX到@rdelmar和这里的一些研究就是答案,如果有人要interessted ;-)

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 

} 
1

日Thnx分享惊人的答案@Seb。由于在Swift中进行了一些更改,这会影响您的答案。我将举例说明如何在Swift 3和Swift 4中执行相同的操作:

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.frame.size.height)) 
    headerView.backgroundColor = UIColor.lightGray 

    let title = UILabel() 
    title.translatesAutoresizingMaskIntoConstraints = false 
    title.text = "myTitle" 
    headerView.addSubview(title) 

    let button = UIButton(type: .system) 
    button.translatesAutoresizingMaskIntoConstraints = false 
    button.setTitle("myButton", for: .normal) 
    headerView.addSubview(button) 

    var headerViews = Dictionary<String, UIView>() 
    headerViews["title"] = title 
    headerViews["button"] = button 

    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[title]-[button]-15-|", options: [], metrics: nil, views: headerViews)) 
    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[title]-|", options: [], metrics: nil, views: headerViews)) 
    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[button]-|", options: [], metrics: nil, views: headerViews)) 

    return headerView 
}