如何使用不同的设备更新CustomView宽度

时间:2018-02-16 23:34:19

标签: ios swift uiview

我有一个带有2个标签和图像的自定义按钮,如下面的课程

所示
import UIKit
@IBDesignable
class CustomButton : UIButton {


    let secondLine : UILabel = UILabel()

    override func layoutSubviews() {
        super.layoutSubviews()
       // self.layer.cornerRadius = 10
        self.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
        self.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        self.layer.shadowOpacity = 1.0
        self.layer.masksToBounds = false


    }

    @IBInspectable var rightLebelText : String?{
        didSet {
            updateView()
        }
    }

    func updateView(){

        if let mytext = rightLebelText {

            let firstLine = UILabel(frame: CGRect(x: self.bounds.size.width - 210, y: 0, width: 200, height: 40))
            firstLine.text = mytext
            firstLine.textAlignment = .right
            firstLine.font = UIFont.systemFont(ofSize: 20)
            self.addSubview(firstLine)
            var imageView : UIImageView
            imageView  = UIImageView(frame:CGRect(x: 5, y: 10, width: 20, height: 20))
            imageView.image = UIImage(named:"arrow.png")
            self.addSubview(imageView)

        }

    }


    public func setSecondlabel(title : String){
        secondLine.removeFromSuperview()
        secondLine.frame = CGRect(x:  50, y: 0, width: 200, height: 40)
        secondLine.text = title
        secondLine.font = UIFont.boldSystemFont(ofSize: 20)
        secondLine.removeFromSuperview()
        self.addSubview(secondLine)
    }
}

我的问题是我的视图尺寸在使用

时未在不同设备上更新
self.bounds.size.width

对于firstLine标签,如下图所示,其位置应位于自定义按钮右边缘

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要覆盖layoutSubviews函数,让每个元素的框架根据自定义视图的更新bounds进行检查和更新,或者在添加元素时为每个元素分配正确的布局约束。

如果要覆盖已具有label和image属性的UIButton,则也可以使用该属性,或者创建从UIControl继承的自定义类,并根据需要创建所需的三个属性。我正在添加一个自定义类的示例,其中包含问题中显示的图像,标题和详细信息。

class CustomButton : UIControl {
    let imageView : UIImageView
    let titleLabel : UILabel
    let detailLabel : UILabel

    fileprivate func setup() {
        self.detailLabel.textAlignment = .right
        self.detailLabel.font = UIFont.systemFont(ofSize: 20)
        self.detailLabel.textColor = UIColor.black
        self.titleLabel.font = UIFont.boldSystemFont(ofSize: 20)
        self.titleLabel.textColor = UIColor.black
        self.backgroundColor = UIColor.white
        self.addSubview(self.imageView)
        self.addSubview(self.titleLabel)
        self.addSubview(self.detailLabel)
    }

    override init(frame: CGRect) {
        self.imageView = UIImageView(frame: .zero)
        self.titleLabel = UILabel(frame: .zero)
        self.detailLabel = UILabel(frame: .zero)
        super.init(frame: frame)
        self.setup()
    }

    required init?(coder aDecoder: NSCoder) {
        self.imageView = UIImageView(frame: .zero)
        self.titleLabel = UILabel(frame: .zero)
        self.detailLabel = UILabel(frame: .zero)
        super.init(coder: aDecoder)
         self.setup()
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        self.imageView.frame = CGRect(x: 5.0, y: self.bounds.midY - 10.0, width: 20.0, height: 20.0)

        //You can make this width dynamic if you want to calculate width of text using self.detailLabel.text
        var width : CGFloat = 200.0
        self.titleLabel.frame = CGRect(x: self.imageView.frame.maxX + 5.0, y: self.bounds.minY, width: 200.0, height: self.bounds.height)
        //Give the remaining space to the second label
        width = self.bounds.width - (self.titleLabel.frame.maxX + 15.0)
        self.detailLabel.frame = CGRect(x: self.titleLabel.frame.maxX + 5.0, y: self.bounds.minY, width: width, height: self.bounds.height)
    }

}