簡體   English   中英

通過代碼將約束設置為具有swift iOS8的Storyboard中的元素

[英]Set constraints through code to an element from Storyboard with swift iOS8

我有一個帶有tableView的ViewController。 我已經在故事板中進行了設置。 有沒有辦法以編程方式設置tableView的約束? 我試圖從ViewController中的tableView設置一個IBOutlet並為其添加約束。 但那沒用。

這是我的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?

謝謝!

您在其中一條評論中提到要在用戶按下按鈕時更改約束。 您可以通過設置約束的出口來實現更少的代碼。 在文檔大綱頁面中找到約束,然后按CTRL +拖動到您的類以創建約束的出口。 然后,您可以更輕松地更改代碼中的常量。

@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
    }
}

通過以下方式更改方法addTableViewConstraints

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)

   NSLayoutConstraint.activateConstraints([topConstraint, leadingContraint, trailingConstraint, bottomConstraint])
}

更新:

您正在運行的NSIBPrototypingLayoutConstraint約束(以及引起異常)由Interface Builder自動生成,以使您的Storyboard或XIB視圖布局不明確。 這樣做非常狡猾,但它會自動添加所需的最小約束,以便完全指定每個模糊視圖的位置和大小。

解決此問題的方法是在Interface Builder中添加所需的最小約束,以便完全指定每個視圖的位置和大小,然后選擇每個不需要的約束,轉到右側邊欄屬性檢查器,並選中Placeholder旁邊的框- 在構建時刪除

我通過以下方式解決了這個問題:

  1. 打開您正在處理的Storyboard視圖控制器。
  2. 選擇你的UITableView
  3. 選擇視圖控制器,然后從菜單中的[] View Controller> Add Missing Constraints中選擇Editor> Resolve Auto Layout Issues> Selected View

在此輸入圖像描述

  1. 從表視圖中選擇所有約束:

在此輸入圖像描述

  1. 從右窗格中選中以下復選框: 占位符 - 在構建時刪除

在此輸入圖像描述

現在,您可以在代碼中手動添加所有自動布局約束,而不會發出任何警告。

我希望這對你有幫助。

假設你有一個名為myButton的按鈕,你想通過代碼創建它的Bottom Space約束。 該約束的constant將設置為500

這是NSLayoutConstraint在Swift中的樣子。 確保將此代碼包含在函數中。

    var constraintButton = NSLayoutConstraint (item: myButton,
        attribute: NSLayoutAttribute.Bottom,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.view,
        attribute: NSLayoutAttribute.Bottom,
        multiplier: 1,
        constant: 500)

    // Add the constraint to the view
    self.view.addConstraint(constraintButton)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM