簡體   English   中英

更新imageView swift 4的高度約束時,無法同時滿足約束

[英]Unable to simultaneously satisfy constraints when update height constraints of imageView swift 4

我有一個stack view ,如下圖所示:

在此處輸入圖片說明

所以我改變的高度image編程,使其適合從我的服務器下載,如果不要有圖像,高度約束的圖像image將設置為零。

這是我這樣做的代碼:

    let imageUrl = URL(string :imageString)

    if let data = try? Data(contentsOf: imageUrl!)
    {

       guard let actualImage: UIImage = UIImage(data: data) else{
        print("No image!")
        ImageView.image = nil
       defaultImageHeightContrainst = ImageHeightContrainst.constant
       ImageHeightContrainst.constant = 0

        layoutIfNeeded()
        return
      }

     let imageHeight = actualImage.size.height * actualImage.scale
     print("imageHeight = \(imageHeight)")
     defaultImageHeightContrainst = ImageHeightContrainst.constant
     ImageHeightContrainst.constant = imageHeight

     layoutIfNeeded()

     //here display the image to the imageview
      ImageView.kf.setImage(with: imageUrl)

   }

使用上面的代碼,圖像高度將根據從互聯網下載的實際圖像高度進行縮放。如果沒有圖像,則“圖像”部分也已設置為0。

這是我所期望的,但是現在每當“圖像”高度變高以適合實際圖像高度時,我都會在下面遇到錯誤

 Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
(
    "<NSLayoutConstraint:0x6080002868b0 UIImageView:0x7f9cbae59240.height == 50   (active)>",
    "<NSLayoutConstraint:0x608000286bd0 UIView:0x7f9cbae5ad80.height == 1   (active)>",
    "<NSLayoutConstraint:0x608000286d10 UIImageView:0x7f9cbae5b440.height == 458   (active)>",
    "<NSLayoutConstraint:0x608000286f90 UIView:0x7f9cbae5bb50.height == 1   (active)>",
    "<NSLayoutConstraint:0x6080002871c0 V:|-(0)-[UILabel:0x7f9cbae5b160]   (active, names: '|':UIStackView:0x7f9cbae5af60 )>",
    "<NSLayoutConstraint:0x608000287350 V:[UILabel:0x7f9cbae5b160]-(10)-[UIImageView:0x7f9cbae5b440]   (active)>",
    "<NSLayoutConstraint:0x6080002873a0 V:[UIImageView:0x7f9cbae5b440]-(10)-[UIStackView:0x7f9cbae5b670]   (active)>",
    "<NSLayoutConstraint:0x608000287580 V:[UIStackView:0x7f9cbae5b670]-(10)-[UIView:0x7f9cbae5bb50]   (active)>",
    "<NSLayoutConstraint:0x6080002875d0 V:[UIStackView:0x7f9cbae5c0f0]-(0)-|   (active, names: '|':UIStackView:0x7f9cbae5af60 )>",
    "<NSLayoutConstraint:0x608000287670 V:[UIView:0x7f9cbae5bb50]-(10)-[UIStackView:0x7f9cbae5c0f0]   (active)>",
    "<NSLayoutConstraint:0x6080002877b0 V:|-(10)-[UIImageView:0x7f9cbae59240]   (active, names: '|':UITableViewCellContentView:0x7f9cbae5a0c0 )>",
    "<NSLayoutConstraint:0x6080002878f0 V:[UIImageView:0x7f9cbae59240]-(8)-[UIView:0x7f9cbae5ad80]   (active)>",
    "<NSLayoutConstraint:0x6080002879e0 V:[UIStackView:0x7f9cbae5af60]-(10)-|   (active, names: '|':UITableViewCellContentView:0x7f9cbae5a0c0 )>",
    "<NSLayoutConstraint:0x608000287a80 V:[UIView:0x7f9cbae5ad80]-(8)-[UIStackView:0x7f9cbae5af60]   (active)>",
    "<NSLayoutConstraint:0x608000288c00 'UISV-canvas-connection' UIStackView:0x7f9cbae5c0f0.top == _UILayoutSpacer:0x6080001cd5c0'UISV-alignment-spanner'.top   (active)>",
    "<NSLayoutConstraint:0x608000288ca0 'UISV-canvas-connection' V:[_UILayoutSpacer:0x6080001cd5c0'UISV-alignment-spanner']-(0)-|   (active, names: '|':UIStackView:0x7f9cbae5c0f0 )>",
    "<NSLayoutConstraint:0x608000289970 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f9cbae5a0c0.height == 476.5   (active)>"
)

在詢問之前,我已經檢查了所有其他約束,這些約束都已正確設置。我什至禁用了所有可以更改“圖像”高度的代碼,一旦禁用,就沒有問題。錯誤僅在我打算更改時發生高度。

我什至嘗試在layoutIfNeeded()之前添加ImageView.translatesAutoresizingMaskIntoConstraints = false ,但是錯誤仍然存​​在。

那么,為了適應從服務器下載的實際圖像,更改圖像高度的正確方法是什么?

考慮到最后一個約束說

"<NSLayoutConstraint:0x608000289970 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x7f9cbae5a0c0.height == 476.5   (active)>"

我假設您正在UITableViewCell內部使用stackViewtableView實現自動高度單元。 如果我的假設是正確的,那么這個問題是不是與stackView ,也不與imageView ,但與方式UITableView可與UITableViewAutomaticDimension和自動布局。 如果布局按預期工作,並且警告是唯一使您煩惱的事情,請閱讀以下內容。

因此在我看來,這是一個已知的“錯誤”的結果-存在通過設定高度的碰撞tableView並通過自動版式計算的高度。 渲染單元格時, tableView首先應用默認高度,計算自動布局高度,然后使用后者-至少看起來如此。 看看我的問題 上面提到的約束( 'UIView-Encapsulated-Layout-Height' )是UITableView應用的約束,后來消失了。

這意味着您正在使用的約束可能還可以。 只需將定義高度的約束之一設置為priority = 999 (這樣,在取消激活默認高度約束之前,不會引起任何沖突)。 最后,它仍然會導致您使用約束,因此不會引起任何布局問題。

例如,如果將stackView約束為適合單元格的contentView ,則將stackView.bottomAnchor設置為contentView.bottomAnchor ,並且優先級設置為999。如果以編程方式進行布局,則這可能是您的解決方案:

let bottomConstraint = tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
bottomConstraint.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
    // rest of the constraints
    stackView.topAnchor.constraint(equalTo: contentView.topAnchor),
    stackView.leftAnchor.constraint(equalTo: contentView.leftAnchor),
    stackView.rightAnchor.constraint(equalTo: contentView.rightAnchor),
    bottomConstraint,
])

如果在情節提要板上進行布局,只需在情節提要中選擇適當的約束,然后在屬性檢查器中將其優先級設置為999(例如):

將約束的優先級設置為999

暫無
暫無

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

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