通过代码将约束设置为具有swift iOS8的Storyboard中的元素 [英] Set constraints through code to an element from Storyboard with swift iOS8

查看:164
本文介绍了通过代码将约束设置为具有swift iOS8的Storyboard中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有tableView的ViewController。我已经在故事板中进行了设置。有没有办法以编程方式设置tableView的约束?我试图从ViewController中的tableView设置一个IBOutlet并为其添加约束。但这没效果。

I have a ViewController with a tableView. I've set it up in the Storyboard. Is there a way to set the constraints for the tableView programmatically? I've tried to set a IBOutlet from my tableView in the ViewController and added constraints to it. But that didn't work.

这是我的ViewController

This is my ViewController

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()

    addTableViewConstraints()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func addTableViewConstraints() {

    tableView.setTranslatesAutoresizingMaskIntoConstraints(false)

    var topConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 20)
    var leadingContraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0)
    var trailingConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0)
    var bottomConstraint = NSLayoutConstraint(item: self.tableView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)

    self.view.addConstraints([topConstraint, leadingContraint, trailingConstraint, bottomConstraint])

}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! UITableViewCell
    cell.textLabel?.text = "Cell"
    return cell
}
}

或者我是否必须通过代码制作我的tableView?

Or do I have to make my tableView through code as well?

谢谢!

推荐答案

您在其中一条评论中提到要在用户按下按钮时更改约束。您可以通过制定约束来减少代码,从而实现此目的。在文档大纲页面中找到约束,然后按CTRL +拖动到您的类以创建约束的出口。然后你可以更容易地更改代码中的常量。

You mentioned in one of your comments that you want to change the constraints when the user presses a button. You can achieve this in less code by making an outlet to the constraint. Find the constraint in the document outline page and CTRL + Drag to your class to create an outlet to the constraint. Then you can change the constant in code easier.

@IBOutlet weak var topConstraint: NSLayoutConstraint!
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!

func buttonPressed() {
    if (/* some condition */) {
        self.topConstraint.constant += 8
        self.bottomConstraint.constant += 8
    } else {
        // Return the constraints to normal, or whatever you want to do
    }
}

这篇关于通过代码将约束设置为具有swift iOS8的Storyboard中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆