代码之家  ›  专栏  ›  技术社区  ›  Mohammed Riyadh

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

  •  0
  • Mohammed Riyadh  · 技术社区  · 7 年前

    我有一个带有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
    

    对于下图所示的第一行标签,其位置应位于自定义按钮的右边缘

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   deoKasuhal    7 年前

    您需要覆盖 layoutSubviews 函数有每个元素的框架检查和更新基于更新 bounds 或在添加每个图元时为其指定适当的布局约束。

    如果要覆盖 UIButton 它已经有一个label和image属性,您也可以使用该属性,或者创建一个从 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)
        }
    
    }