Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to auto layout contentView? #231

Open
iOkay opened this issue Aug 6, 2018 · 2 comments
Open

How to auto layout contentView? #231

iOkay opened this issue Aug 6, 2018 · 2 comments

Comments

@iOkay
Copy link

iOkay commented Aug 6, 2018

I called PKHUD show content view as follow, but content view's frame is zero, cannot show on view. How to deal with the frame of contentView without set one? Is it possible to auto layout the content View?

 let content = TipMessageView(type: type)
            content.label.text = message
            PKHUD.sharedHUD.contentView = content
            
            PKHUD.sharedHUD.show()
            PKHUD.sharedHUD.hide(afterDelay: TimeInterval(delaySec), completion: { (success) in
            
                if completion != nil {
                    DispatchQueue.main.async {
                        completion!()
                    }
                }
            })

Code of TipMessageView:

class TipMessageView: UIView {

    private var leftView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }()
    
    private var rightView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.clear
        return view
    }()
    
    private var icon: UIImageView = {
        let view = UIImageView()
        view.backgroundColor = UIColor.clear
        return view
    }()
    var label: UILabel = {
        let label = UILabel()
        label.backgroundColor = UIColor.clear
        label.textAlignment = .center
        label.font = UIFont.PingFangSC_Semibold(size: 14)
        label.textColor = UIColor.white
        return label
    }()
    
    private var _type: TipMessageType = .success
    var type: TipMessageType {
        get {
            return _type
        }
        set(newValue) {
            _type = newValue
            if _type == .success {
                
                self.backgroundColor = UIColor(hexString: "#37B0CD")
                self.icon.image = UIImage(named: "tip_hud_suc")
            } else if _type == .fail {
                
                self.backgroundColor = UIColor(hexString: "#F26868")
                self.icon.image = UIImage(named: "tip_hud_fail")
            }
        }
    }
    
    init(type: TipMessageType) {
        
        //CGRect(x:0, y:0, width:204, height: 56)
        super.init(frame: CGRectZero)
        
        self.layer.cornerRadius = 4
        self.clipsToBounds = true
        
        self.addSubview(self.leftView)
        self.addSubview(self.icon)
        self.addSubview(self.label)
        self.addSubview(self.rightView)
        
        self.type = type
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        self.leftView.snp.makeConstraints { (make) in
            
            make.left.equalTo(self)
            make.centerY.equalTo(self)
            make.width.equalTo(self.rightView)
            make.height.equalTo(1)
        }
        
        self.icon.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(self.leftView.snp.right)
            make.width.equalTo(19)
            make.height.equalTo(19)
        }
        
        self.label.snp.makeConstraints { (make) in
            make.centerY.equalTo(self)
            make.left.equalTo(self.icon.snp.right).offset(4)
        }
        
        self.rightView.snp.makeConstraints { (make) in
            
            make.centerY.equalTo(self)
            make.left.equalTo(self.label.snp.right)
            make.right.equalTo(self)
        }
    }
}

@jprothwell
Copy link

same qus

1 similar comment
@chinghoi
Copy link

same qus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants