簡體   English   中英

UIView不能根據子視圖調整寬度?

[英]UIView not adjusting width according to subviews?

我正在嘗試創建一個可重用的UIView subclass ,並將UILabelUIImageView作為subviews 我的視圖子類應根據標簽的寬度調整其寬度。

這是我的課

class CustomView: UIView {
    private var infoLabel: UILabel!
    private var imageView: UIImageView!


    override init(frame: CGRect) {
        super.init(frame: frame)
        infoLabel = UILabel(frame: CGRect.zero)
        imageView = UIImageView(frame: CGRect.zero)
        addSubview(infoLabel)
        addSubview(imageView)

        infoLabel.backgroundColor = .white
        imageView.backgroundColor = .gray
    }
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func updateConstraints() {
        super.updateConstraints()
        infoLabel.translatesAutoresizingMaskIntoConstraints = false
        imageView.translatesAutoresizingMaskIntoConstraints = false

        infoLabel.leadingAnchor.constraint(
            equalTo: self.leadingAnchor, constant: 5).isActive = true
        // infoLabel.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        infoLabel.topAnchor.constraint(equalTo: self.topAnchor, constant: 5).isActive = true
        infoLabel.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 5).isActive = true

        imageView.leadingAnchor.constraint(
            equalTo: infoLabel.trailingAnchor, constant: 10).isActive = true
        //imageView.trailingAnchor.constraint(
            //equalTo: self.trailingAnchor, constant: 10).isActive = true
        //imageView.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        imageView.widthAnchor.constraint(equalToConstant: 25).isActive = true
        imageView.topAnchor.constraint(equalTo: self.topAnchor, constant: 5).isActive = true
        imageView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 5).isActive = true

    }
    internal func setText(_ text: String, andImage image: String){
        infoLabel.text = text
        imageView.image = UIImage(named: image)
    }
}

這就是我將其添加到視圖中的方式-

let aView: CustomView = CustomView(frame: CGRect(x: 20, y: 144, width: 120, height: 31))
view.addSubview(aView)
       aView.setText("my testing label", andImage: "distanceIcon")
        aView.backgroundColor = UIColor.red

我得到像添加圖像的結果。(紅色是我的自定義視圖,白色是標簽,灰色是圖像) 在此處輸入圖片說明

編輯:如果在Storyboard中添加視圖,則可以使用,但是如果我通過上述代碼嘗試運行,則無法使用。

您還必須在CustomView類實例上調用translationsAutoresizingMaskIntoConstraints = false:

self.translatesAutoresizingMaskIntoConstraints = false

還要在自定義視圖上添加前導和頂部約束以設置其位置:

customView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 200).isActive = true
customView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 200).isActive = true

不要忘記在imageView上添加尾隨約束:

imageView.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -10).isActive = true

暫無
暫無

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

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