簡體   English   中英

將Leading space設置為UIView,立即崩潰

[英]Set Leading space to UIView , Crashes in swift

我需要修改View的前導空間 這是我的代碼

    let advSearchViewAddLeadingConstraint: NSLayoutConstraint = NSLayoutConstraint(
        item:advancedSearchView,
        attribute:NSLayoutAttribute.Leading,
        relatedBy:NSLayoutRelation.Equal,
        toItem:self.view, 
        attribute:NSLayoutAttribute.NotAnAttribute,
        multiplier:0,
        constant:200)
        self.view.addConstraint(advSearchViewAddLeadingConstraint)

通過使用此代碼,我崩潰了

*由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'* + [NSLayoutConstraint ConstrainintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:未知的布局屬性'

我會反過來工作。 創建一個包含視圖並拖動IBOutlet以限制空間。 然后只要更改常數,它應該會更好地工作。

leadingConstraint.constant = 200
self.view.layoutIfNeeded()

您可能需要NSLayoutAttribute.Leading而不是NSLayoutAttribute.NotAnAttribute 如果要提供兩個項目,則需要指定每個項目的屬性:

let advSearchViewAddLeadingConstraint: NSLayoutConstraint =
    NSLayoutConstraint(
        item:advancedSearchView,
        attribute:NSLayoutAttribute.Leading,
        relatedBy:NSLayoutRelation.Equal,
        toItem:self.view, 
        attribute:NSLayoutAttribute.Leading,
        multiplier:0,
        constant:200)

其他一些注意事項:

  • 您可以使用以下方法激活約束: advSearchViewAddLeadingConstraint.active = true

  • 在Swift中,類型推斷可以幫助您使用枚舉類型,因此無需鍵入NSLayoutAttribute.Leading即可鍵入Leading

暫無
暫無

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

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